Exemplo n.º 1
0
 public ArrayMarshalInfo(NativeType elementType, int? paramNum, int? numParams)
     : base(NativeType.Array)
 {
     m_elementType = elementType;
     m_paramNum = paramNum;
     m_numParams = numParams;
 }
Exemplo n.º 2
0
 public ArrayMarshalInfo()
     : base(NativeType.Array)
 {
     element_type = NativeType.None;
     size_parameter_index = -1;
     size = -1;
 }
Exemplo n.º 3
0
 public object Getvalue()
 {
     return(NativeType.GetValue(_memHolder, this, 0, true));
 }
Exemplo n.º 4
0
 internal static bool IsFloatingPoint(this NativeType t)
 {
     return(t == NativeType.Single || t == NativeType.Double || t == NativeType.Decimal);
 }
Exemplo n.º 5
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="nativeType">Native type</param>
		/// <param name="iidParamIndex"></param>
		public InterfaceMarshalType(NativeType nativeType, int iidParamIndex)
			: base(nativeType) {
			if (nativeType != NativeType.IUnknown &&
				nativeType != NativeType.IDispatch &&
				nativeType != NativeType.IntF)
				throw new ArgumentException("Invalid nativeType");
			this.iidParamIndex = iidParamIndex;
		}
Exemplo n.º 6
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="elementType">Element type</param>
		/// <param name="paramNum">Parameter number</param>
		/// <param name="numElems">Number of elements</param>
		public ArrayMarshalType(NativeType elementType, int paramNum, int numElems)
			: this(elementType, paramNum, numElems, -1) {
		}
Exemplo n.º 7
0
 private void Write(NativeType type)
 {
     _writer.Write((byte)type);
 }
Exemplo n.º 8
0
		public MarshalSig (NativeType nt)
		{
			this.NativeInstrinsic = nt;
		}
Exemplo n.º 9
0
 internal ConstantWithValue(T value, NativeType nt)
 {
     Value = value; Type = Compilation.Get(nt);
 }
Exemplo n.º 10
0
 internal static ConstantWithValue <T> Create <T>(T value, NativeType nt)
 {
     return(new ConstantWithValue <T>(value, nt));
 }
Exemplo n.º 11
0
        public MarshalInfo ReadMarshalInfo()
        {
            NativeType native = this.ReadNativeType();
            NativeType type2  = native;

            if (type2 == NativeType.FixedSysString)
            {
                FixedSysStringMarshalInfo info4 = new FixedSysStringMarshalInfo();
                if (this.CanReadMore())
                {
                    info4.size = (int)base.ReadCompressedUInt32();
                }
                return(info4);
            }
            switch (type2)
            {
            case NativeType.SafeArray:
            {
                SafeArrayMarshalInfo info2 = new SafeArrayMarshalInfo();
                if (this.CanReadMore())
                {
                    info2.element_type = this.ReadVariantType();
                }
                return(info2);
            }

            case NativeType.FixedArray:
            {
                FixedArrayMarshalInfo info3 = new FixedArrayMarshalInfo();
                if (this.CanReadMore())
                {
                    info3.size = (int)base.ReadCompressedUInt32();
                }
                if (this.CanReadMore())
                {
                    info3.element_type = this.ReadNativeType();
                }
                return(info3);
            }
            }
            switch (type2)
            {
            case NativeType.Array:
            {
                ArrayMarshalInfo info = new ArrayMarshalInfo();
                if (this.CanReadMore())
                {
                    info.element_type = this.ReadNativeType();
                }
                if (this.CanReadMore())
                {
                    info.size_parameter_index = (int)base.ReadCompressedUInt32();
                }
                if (this.CanReadMore())
                {
                    info.size = (int)base.ReadCompressedUInt32();
                }
                if (this.CanReadMore())
                {
                    info.size_parameter_multiplier = (int)base.ReadCompressedUInt32();
                }
                return(info);
            }

            case NativeType.CustomMarshaler:
            {
                CustomMarshalInfo info5 = new CustomMarshalInfo();
                string            str   = this.ReadUTF8String();
                info5.guid           = !string.IsNullOrEmpty(str) ? new Guid(str) : Guid.Empty;
                info5.unmanaged_type = this.ReadUTF8String();
                info5.managed_type   = this.ReadTypeReference();
                info5.cookie         = this.ReadUTF8String();
                return(info5);
            }
            }
            return(new MarshalInfo(native));
        }
Exemplo n.º 12
0
 internal static bool TypesMatch(TypeSymbol t, NativeType nt) => t.NativeType == nt;
Exemplo n.º 13
0
        /// <summary>
        /// Generate the NativeMember
        /// </summary>
        /// <param name="nt"></param>
        /// <param name="ctd"></param>
        /// <remarks></remarks>
        private CodeMemberField GenerateContainerMember(NativeMember nt, CodeTypeDeclaration ctd)
        {
            ThrowIfNull(nt);
            ThrowIfNull(ctd);
            ThrowIfTrue(nt.NativeType.Kind == NativeSymbolKind.BitVectorType);
            // Bitvector instances should be handled seperately

            // Generate the type reference and comment
            string          comment = string.Empty;
            CodeMemberField member  = new CodeMemberField();

            member.Name       = nt.Name;
            member.Type       = GenerateTypeReferenceImpl(nt.NativeType, ref comment);
            member.Attributes = MemberAttributes.Public;
            member.Comments.Add(new CodeCommentStatement(comment, true));
            member.UserData.Add(TransformConstants.Member, nt);
            ctd.Members.Add(member);

            // If this is an array then add the appropriate marshal directive if it's an inline array
            NativeArray ntArray = nt.NativeType as NativeArray;

            if (ntArray != null && ntArray.ElementCount > 0)
            {
                // Add the struct layout attribute
                CodeTypeReference        attrRef = new CodeTypeReference(typeof(MarshalAsAttribute));
                CodeAttributeDeclaration attr    = new CodeAttributeDeclaration(attrRef);

                // ByValArray
                CodeAttributeArgument asArg = new CodeAttributeArgument();
                asArg.Name  = string.Empty;
                asArg.Value = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(UnmanagedType)), "ByValArray");
                attr.Arguments.Add(asArg);

                // SizeConst arg
                CodeAttributeArgument sizeArg = new CodeAttributeArgument();
                sizeArg.Name  = "SizeConst";
                sizeArg.Value = new CodePrimitiveExpression(ntArray.ElementCount);
                attr.Arguments.Add(sizeArg);

                // ArraySubType
                NativeType            elemType   = ntArray.RealTypeDigged;
                CodeAttributeArgument subTypeArg = new CodeAttributeArgument();
                subTypeArg.Name = "ArraySubType";
                if (elemType.Kind == NativeSymbolKind.BuiltinType)
                {
                    // Builtin types know their size in bytes
                    NativeBuiltinType elemBt = (NativeBuiltinType)elemType;
                    subTypeArg.Value = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(UnmanagedType)), elemBt.UnmanagedType.ToString());
                }
                else if (elemType.Kind == NativeSymbolKind.PointerType || elemType.Kind == NativeSymbolKind.ArrayType)
                {
                    // Marshal pointers as system ints
                    subTypeArg.Value = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(UnmanagedType)), "SysUInt");
                }
                else
                {
                    subTypeArg.Value = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(UnmanagedType)), "Struct");
                }
                attr.Arguments.Add(subTypeArg);

                member.CustomAttributes.Add(attr);
            }

            return(member);
        }
Exemplo n.º 14
0
        private bool IsBitVector(NativeType nt)
        {
            NativeBitVector bt = null;

            return(IsBitVector(nt, ref bt));
        }
Exemplo n.º 15
0
 private void Write(NativeType type)
 {
     _writer.Write((byte)type);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Remove any marshalling information for this parameter
 /// </summary>
 public void RemoveMashalType()
 {
     marshalType = null; parMode &= noFieldMarshal;
 }
Exemplo n.º 17
0
 public MarshalInfo(NativeType nativeType)
 {
     m_nativeType = nativeType.CheckDefined("nativeType");
 }
Exemplo n.º 18
0
 public MarshalSig(NativeType nt)
 {
     this.NativeInstrinsic = nt;
 }
Exemplo n.º 19
0
 public ArrayMarshalDescriptor(NativeType elementType)
 {
     ElementType = elementType;
 }
Exemplo n.º 20
0
 public FixedArray()
 {
     this.NumElem       = 0;
     this.ArrayElemType = NativeType.NONE;
 }
Exemplo n.º 21
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="elementType">Element type</param>
		public ArrayMarshalType(NativeType elementType)
			: this(elementType, -1, -1, -1) {
		}
Exemplo n.º 22
0
 public SimpleMarshalDescriptor(NativeType nativeType)
 {
     _nativeType = nativeType;
 }
Exemplo n.º 23
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="nativeType">Native type</param>
		public MarshalType(NativeType nativeType) {
			this.nativeType = nativeType;
		}
Exemplo n.º 24
0
 /// <summary>
 /// Try and find any global symbol with the specified name.
 /// </summary>
 public static bool TryGetType(this INativeSymbolLookup lookup, string name, out NativeType nt)
 {
     return(lookup.TryGetGlobalSymbol(name, out nt));
 }
Exemplo n.º 25
0
		public FixedArrayMarshalInfo ()
			: base (NativeType.FixedArray)
		{
			element_type = NativeType.None;
		}
 /// <summary>
 /// Creates a new instance of the <see cref="FixedArrayMarshalDescriptor"/> class.
 /// </summary>
 /// <param name="size">The fixed size of the array.</param>
 /// <param name="arrayElementType">The type each element in the array should be marshalled as.</param>
 public FixedArrayMarshalDescriptor(int size, NativeType arrayElementType)
 {
     Size             = size;
     ArrayElementType = arrayElementType;
 }
Exemplo n.º 27
0
        /// <summary>
        /// Initializes the array as a <see cref="UnmanagedType.LPArray"/>.
        /// </summary>
        private void InitializeAsNativeArray(NativeTypeDesc desc)
        {
            this.arrayKind = ArrayKind.NativeArray;

            if (desc.IsByRefParam)
            {
                // SizeConst and SizeParamIndex are not allowed for byref
                if (HasSizeConst(desc) || HasSizeParamIndex(desc))
                {
                    Log.Add(Errors.ERROR_ArraySizeNotAllowedForByref);
                }
            }
            else if (!desc.IsCallbackParam)
            {
                // when marshaling from managed to native, size is implicit (warn if given explicitly)
                if (HasSizeConst(desc) || HasSizeParamIndex(desc))
                {
                    Log.Add(Errors.WARN_ArraySizesIgnored);
                }
                else
                {
                    Log.Add(Errors.INFO_ArraySizeDeterminedDynamically);
                }
            }
            else
            {
                // when marshaling from native to managed, size must be given, otherwise it's 1
                if (HasSizeParamIndex(desc))
                {
                    Debug.Assert(desc.ParameterInfo != null);
                    ParameterInfo[] parameters = ((MethodBase)desc.ParameterInfo.Member).GetParameters();

                    int param_index = desc.MarshalAs.SizeParamIndex;
                    if (param_index < 0 || param_index >= parameters.Length)
                    {
                        // index OOR (error)
                        Log.Add(Errors.ERROR_ArraySizeParamIndexOutOfRange, param_index);
                    }
                    else if (!TypeAllowedInSizeParam(parameters[param_index].ParameterType))
                    {
                        // index refers to bad param (error)
                        Log.Add(Errors.ERROR_ArraySizeParamWrongType,parameters[param_index].ParameterType);
                    }
                    else
                    {
                        // determine parameter name
                        string param_name = parameters[param_index].Name;
                        if (String.IsNullOrEmpty(param_name))
                        {
                            param_name = String.Format(Resources.Number, param_index + 1);
                        }

                        if (desc.MarshalAs.SizeConst > 0)
                        {
                            // size = [param_at_the_index] + size_const;
                            Log.Add(Errors.INFO_ArraySizeIsByParameterPlusConstant,
                                param_name, desc.MarshalAs.SizeConst);
                        }
                        else
                        {
                            // size = [param_at_the_index]
                            Log.Add(Errors.INFO_ArraySizeIsByParameter, param_name);
                        }
                    }
                }
                else if (HasSizeConst(desc))
                {
                    // size = size_const
                    Log.Add(Errors.INFO_ArraySizeIsConstant, desc.MarshalAs.SizeConst);
                }
                else
                {
                    // size = 1 (warn)
                    Log.Add(Errors.WARN_ArraySizeDefaultsToOne);
                }
            }

            UnmanagedType element_unmng_type =
                (desc.MarshalAs == null ? (UnmanagedType)0 : desc.MarshalAs.ArraySubType);
            if (element_unmng_type == (UnmanagedType)80) element_unmng_type = (UnmanagedType)0;

            // determine the element type
            this.elementType = NativeType.FromClrArrayElement(desc.Type, element_unmng_type, desc.Flags);

            ExplainMemoryManagement(desc, Resources._Array);
        }
Exemplo n.º 28
0
        protected override void EmitMarshalFieldManagedToNative()
        {
            // It generates the following code
            //if (ManagedArg.Field != null)
            //{
            //
            //  fixed (InlineArray* pUnsafe = &NativeArg.Field)
            //  {
            //        uint index = 0u;
            //        while ((ulong)index < (ulong)((long)ManagedArg.Field.Length))
            //        {
            //            NativeArg.s[index] = ManagedArg.Field[(int)index];
            //            index += 1u;
            //        }
            //  }
            //}

            ILEmitter    emitter         = _ilCodeStreams.Emitter;
            ILCodeStream codeStream      = _ilCodeStreams.MarshallingCodeStream;
            var          nativeArrayType = NativeType as InlineArrayType;

            Debug.Assert(nativeArrayType != null);
            Debug.Assert(ManagedType is ArrayType);

            var managedElementType = ((ArrayType)ManagedType).ElementType;

            ILCodeLabel lDone       = emitter.NewCodeLabel();
            ILCodeLabel lRangeCheck = emitter.NewCodeLabel();
            ILCodeLabel lLoopHeader = emitter.NewCodeLabel();

            ILLocalVariable vIndex  = emitter.NewLocal(Context.GetWellKnownType(WellKnownType.Int32));
            ILLocalVariable vLength = emitter.NewLocal(Context.GetWellKnownType(WellKnownType.Int32));
            ILLocalVariable vNative = emitter.NewLocal(NativeType.MakeByRefType(), isPinned: true);

            // check if ManagedType == null, then return
            codeStream.EmitLdArg(0);
            codeStream.Emit(ILOpcode.ldfld, emitter.NewToken(_managedField));
            codeStream.Emit(ILOpcode.brfalse, lDone);

            codeStream.EmitLdArg(1);
            codeStream.Emit(ILOpcode.ldflda, emitter.NewToken(_nativeField));
            codeStream.EmitStLoc(vNative);

            EmitElementCount(codeStream, MarshalDirection.Forward);
            codeStream.EmitStLoc(vLength);

            codeStream.EmitLdc(0);
            codeStream.EmitStLoc(vIndex);
            codeStream.Emit(ILOpcode.br, lRangeCheck);

            codeStream.EmitLabel(lLoopHeader);
            codeStream.EmitLdArg(1);
            codeStream.Emit(ILOpcode.ldflda, emitter.NewToken(_nativeField));
            codeStream.EmitLdLoc(vIndex);

            codeStream.EmitLdArg(0);
            codeStream.Emit(ILOpcode.ldfld, emitter.NewToken(_managedField));
            codeStream.EmitLdLoc(vIndex);

            codeStream.EmitLdElem(managedElementType);

            // generate marshalling IL for the element
            GetElementMarshaller(MarshalDirection.Forward)
            .EmitMarshallingIL(new PInvokeILCodeStreams(_ilCodeStreams.Emitter, codeStream));

            codeStream.Emit(ILOpcode.call, emitter.NewToken(
                                nativeArrayType.GetInlineArrayMethod(InlineArrayMethodKind.Setter)));

            codeStream.EmitLdLoc(vIndex);
            codeStream.EmitLdc(1);
            codeStream.Emit(ILOpcode.add);
            codeStream.EmitStLoc(vIndex);

            codeStream.EmitLabel(lRangeCheck);
            codeStream.EmitLdLoc(vIndex);

            codeStream.EmitLdLoc(vLength);
            codeStream.Emit(ILOpcode.blt, lLoopHeader);

            codeStream.EmitLabel(lDone);
        }
Exemplo n.º 29
0
 private string GetDataRepr(CodeContext /*!*/ context)
 {
     return(PythonOps.Repr(context, NativeType.GetValue(_memHolder, this, 0, false)));
 }
Exemplo n.º 30
0
 /// <summary>
 /// Creates a new instance of the <see cref="LPArrayMarshalDescriptor"/> class.
 /// </summary>
 /// <param name="arrayElementType">The type of elements stored in the array.</param>
 public LPArrayMarshalDescriptor(NativeType arrayElementType)
 {
     ArrayElementType = arrayElementType;
 }
 private void WriteNativeType(NativeType nativeType, MarshalInfo marshalInfo = null)
 {
     switch (nativeType)
     {
         case NativeType.None:
             break;
         case NativeType.Boolean:
             WriteKeyword("bool");
             break;
         case NativeType.I1:
             WriteKeyword("int8");
             break;
         case NativeType.U1:
             WriteKeyword("unsigned int8");
             break;
         case NativeType.I2:
             WriteKeyword("int16");
             break;
         case NativeType.U2:
             WriteKeyword("unsigned int16");
             break;
         case NativeType.I4:
             WriteKeyword("int32");
             break;
         case NativeType.U4:
             WriteKeyword("unsigned int32");
             break;
         case NativeType.I8:
             WriteKeyword("int64");
             break;
         case NativeType.U8:
             WriteKeyword("unsigned int64");
             break;
         case NativeType.R4:
             WriteKeyword("float32");
             break;
         case NativeType.R8:
             WriteKeyword("float64");
             break;
         case NativeType.LPStr:
             WriteKeyword("lpstr");
             break;
         case NativeType.Int:
             WriteKeyword("int");
             break;
         case NativeType.UInt:
             WriteKeyword("unsigned int");
             break;
         case NativeType.Func:
             goto default;
         case NativeType.Array:
             ArrayMarshalInfo ami = (ArrayMarshalInfo)marshalInfo;
             if (ami == null)
                 goto default;
             if (ami.ElementType != NativeType.Max)
                 WriteNativeType(ami.ElementType);
             WriteToken("[");
             if (ami.SizeParameterMultiplier == 0)
             {
                 WriteLiteral(ami.Size.ToString());
             }
             else
             {
                 if (ami.Size >= 0)
                     WriteLiteral(ami.Size.ToString());
                 WriteSpace();
                 WriteToken("+");
                 WriteSpace();
                 WriteLiteral(ami.SizeParameterIndex.ToString());
             }
             WriteToken("]");
             break;
         case NativeType.Currency:
             WriteKeyword("currency");
             break;
         case NativeType.BStr:
             WriteKeyword("bstr");
             break;
         case NativeType.LPWStr:
             WriteKeyword("lpwstr");
             break;
         case NativeType.LPTStr:
             WriteKeyword("lptstr");
             break;
         case NativeType.FixedSysString:
             WriteKeyword(string.Format("fixed sysstring[{0}]", ((FixedSysStringMarshalInfo)marshalInfo).Size));
             break;
         case NativeType.IUnknown:
             WriteKeyword("iunknown");
             break;
         case NativeType.IDispatch:
             WriteKeyword("idispatch");
             break;
         case NativeType.Struct:
             WriteKeyword("struct");
             break;
         case NativeType.IntF:
             WriteKeyword("interface");
             break;
         case NativeType.SafeArray:
             WriteKeyWordPostSpace("safearray");
             SafeArrayMarshalInfo sami = marshalInfo as SafeArrayMarshalInfo;
             if (sami != null)
             {
                 switch (sami.ElementType)
                 {
                     case VariantType.None:
                         break;
                     case VariantType.I2:
                         WriteKeyword("int16");
                         break;
                     case VariantType.I4:
                         WriteKeyword("int32");
                         break;
                     case VariantType.R4:
                         WriteKeyword("float32");
                         break;
                     case VariantType.R8:
                         WriteKeyword("float64");
                         break;
                     case VariantType.CY:
                         WriteKeyword("currency");
                         break;
                     case VariantType.Date:
                         WriteKeyword("date");
                         break;
                     case VariantType.BStr:
                         WriteKeyword("bstr");
                         break;
                     case VariantType.Dispatch:
                         WriteKeyword("idispatch");
                         break;
                     case VariantType.Error:
                         WriteKeyword("error");
                         break;
                     case VariantType.Bool:
                         WriteKeyword("bool");
                         break;
                     case VariantType.Variant:
                         WriteKeyword("variant");
                         break;
                     case VariantType.Unknown:
                         WriteKeyword("iunknown");
                         break;
                     case VariantType.Decimal:
                         WriteKeyword("decimal");
                         break;
                     case VariantType.I1:
                         WriteKeyword("int8");
                         break;
                     case VariantType.UI1:
                         WriteKeyword("unsigned int8");
                         break;
                     case VariantType.UI2:
                         WriteKeyword("unsigned int16");
                         break;
                     case VariantType.UI4:
                         WriteKeyword("unsigned int32");
                         break;
                     case VariantType.Int:
                         WriteKeyword("int");
                         break;
                     case VariantType.UInt:
                         WriteKeyword("unsigned int");
                         break;
                     default:
                         WriteKeyword(sami.ElementType.ToString());
                         break;
                 }
             }
             break;
         case NativeType.FixedArray:
             WriteKeyword("fixed array");
             FixedArrayMarshalInfo fami = marshalInfo as FixedArrayMarshalInfo;
             if (fami != null)
             {
                 WriteToken("[");
                 WriteLiteral(fami.Size.ToString());
                 WriteToken("]");
                 if (fami.ElementType != NativeType.None)
                 {
                     WriteSpace();
                     WriteNativeType(fami.ElementType);
                 }
             }
             break;
         case NativeType.ByValStr:
             WriteKeyword("byvalstr");
             break;
         case NativeType.ANSIBStr:
             WriteKeyword("ansi bstr");
             break;
         case NativeType.TBStr:
             WriteKeyword("tbstr");
             break;
         case NativeType.VariantBool:
             WriteKeyword("variant bool");
             break;
         case NativeType.ASAny:
             WriteKeyword("as any");
             break;
         case NativeType.LPStruct:
             WriteKeyword("lpstruct");
             break;
         case NativeType.CustomMarshaler:
             CustomMarshalInfo cmi = marshalInfo as CustomMarshalInfo;
             if (cmi == null)
                 goto default;
             WriteKeyword("custom");
             WriteToken("(");
             WriteLiteralInQuotes(ConvertString(cmi.ManagedType.FullName));
             WriteTokenPostSpace(",");
             WriteLiteralInQuotes(ConvertString(cmi.Cookie));
             if (cmi.Guid != Guid.Empty || !string.IsNullOrEmpty(cmi.UnmanagedType))
             {
                 WriteTokenPostSpace(",");
                 WriteLiteralInQuotes(cmi.Guid.ToString());
                 WriteTokenPostSpace(",");
                 WriteLiteralInQuotes(ConvertString(cmi.UnmanagedType));
             }
             WriteToken(")");
             break;
         case NativeType.Error:
             WriteKeyword("error");
             break;
         default:
             WriteKeyword(nativeType.ToString());
             break;
     }
 }
Exemplo n.º 32
0
        static bool IsComInterop(IMarshalInfoProvider marshalInfoProvider, TypeReference parameterType, LinkContext context)
        {
            // This is best effort. One can likely find ways how to get COM without triggering these alarms.
            // AsAny marshalling of a struct with an object-typed field would be one, for example.

            // This logic roughly corresponds to MarshalInfo::MarshalInfo in CoreCLR,
            // not trying to handle invalid cases and distinctions that are not interesting wrt
            // "is this COM?" question.

            NativeType nativeType = NativeType.None;

            if (marshalInfoProvider.HasMarshalInfo)
            {
                nativeType = marshalInfoProvider.MarshalInfo.NativeType;
            }

            if (nativeType == NativeType.IUnknown || nativeType == NativeType.IDispatch || nativeType == NativeType.IntF)
            {
                // This is COM by definition
                return(true);
            }

            if (nativeType == NativeType.None)
            {
                // Resolve will look at the element type
                var parameterTypeDef = context.TryResolve(parameterType);

                if (parameterTypeDef != null)
                {
                    if (parameterTypeDef.IsTypeOf(WellKnownType.System_Array))
                    {
                        // System.Array marshals as IUnknown by default
                        return(true);
                    }
                    else if (parameterTypeDef.IsTypeOf(WellKnownType.System_String) ||
                             parameterTypeDef.IsTypeOf("System.Text", "StringBuilder"))
                    {
                        // String and StringBuilder are special cased by interop
                        return(false);
                    }

                    if (parameterTypeDef.IsValueType)
                    {
                        // Value types don't marshal as COM
                        return(false);
                    }
                    else if (parameterTypeDef.IsInterface)
                    {
                        // Interface types marshal as COM by default
                        return(true);
                    }
                    else if (parameterTypeDef.IsMulticastDelegate())
                    {
                        // Delegates are special cased by interop
                        return(false);
                    }
                    else if (parameterTypeDef.IsSubclassOf("System.Runtime.InteropServices", "CriticalHandle", context))
                    {
                        // Subclasses of CriticalHandle are special cased by interop
                        return(false);
                    }
                    else if (parameterTypeDef.IsSubclassOf("System.Runtime.InteropServices", "SafeHandle", context))
                    {
                        // Subclasses of SafeHandle are special cased by interop
                        return(false);
                    }
                    else if (!parameterTypeDef.IsSequentialLayout && !parameterTypeDef.IsExplicitLayout)
                    {
                        // Rest of classes that don't have layout marshal as COM
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 33
0
 /// <summary>
 /// Add marshalling information about this parameter
 /// </summary>
 public void SetMarshalType(NativeType mType)
 {
     marshalType = mType;
     parMode |= hasFieldMarshal;
 }
Exemplo n.º 34
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="nativeType">Native type</param>
 public MarshalType(NativeType nativeType) => this.nativeType = nativeType;
Exemplo n.º 35
0
 public Native(string name, string _namespace, ulong hash, string returnType, Parameter[] _params, NativeType type)
 {
     Name       = name;
     Namespace  = _namespace;
     Hash       = hash;
     Return     = returnType;
     Parameters = _params;
     Type       = type;
 }
 /// <summary>
 /// Creates a new instance of the <see cref="SimpleMarshalDescriptor"/> class.
 /// </summary>
 /// <param name="nativeType"></param>
 public SimpleMarshalDescriptor(NativeType nativeType)
 {
     NativeType = nativeType;
 }
Exemplo n.º 37
0
			public FixedArray ()
			{
				this.NumElem = 0;
				this.ArrayElemType = NativeType.NONE;
			}
Exemplo n.º 38
0
 public FixedArrayMarshalInfo()
     : base(NativeType.FixedArray)
 {
     element_type = NativeType.None;
 }
Exemplo n.º 39
0
 public StringMarshaller(MarshalInfo marshalInfo)
 {
     nativeType = marshalInfo != null ? marshalInfo.NativeType : NativeType.LPTStr;
 }
Exemplo n.º 40
0
 public MarshalInfo(NativeType native)
 {
     this.native = native;
 }
Exemplo n.º 41
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="size">Size</param>
		/// <param name="elementType">Element type</param>
		public FixedArrayMarshalType(int size, NativeType elementType)
			: base(NativeType.FixedArray) {
			this.size = size;
			this.elementType = elementType;
		}
Exemplo n.º 42
0
        private void WriteNativeType(NativeType nativeType, MarshalInfo marshalInfo = null)
        {
            //ncrunch: no coverage start
            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (nativeType)
            {
            case NativeType.None: break;

            case NativeType.Boolean: _o.Write("bool"); break;

            case NativeType.I1: _o.Write("int8"); break;

            case NativeType.U1: _o.Write("unsigned int8"); break;

            case NativeType.I2: _o.Write("int16"); break;

            case NativeType.U2: _o.Write("unsigned int16"); break;

            case NativeType.I4: _o.Write("int32"); break;

            case NativeType.U4: _o.Write("unsigned int32"); break;

            case NativeType.I8: _o.Write("int64"); break;

            case NativeType.U8: _o.Write("unsigned int64"); break;

            case NativeType.R4: _o.Write("float32"); break;

            case NativeType.R8: _o.Write("float64"); break;

            case NativeType.LPStr: _o.Write("lpstr"); break;

            case NativeType.Int: _o.Write("int"); break;

            case NativeType.UInt: _o.Write("unsigned int"); break;

            case NativeType.Func: goto default;                     // ??

            case NativeType.Array: var ami = (ArrayMarshalInfo)marshalInfo;
                if (ami == null)
                {
                    goto default;
                }
                if (ami.ElementType != NativeType.Max)
                {
                    WriteNativeType(ami.ElementType);
                }
                _o.Write('[');
                if (ami.SizeParameterMultiplier == 0)
                {
                    _o.Write(ami.Size.ToString());
                }
                else
                {
                    if (ami.Size >= 0)
                    {
                        _o.Write(ami.Size.ToString());
                    }
                    _o.Write(" + ");
                    _o.Write(ami.SizeParameterIndex.ToString());
                }
                _o.Write(']'); break;

            case NativeType.Currency: _o.Write("currency"); break;

            case NativeType.BStr: _o.Write("bstr"); break;

            case NativeType.LPWStr: _o.Write("lpwstr"); break;

            case NativeType.LPTStr: _o.Write("lptstr"); break;

            // ReSharper disable once PossibleNullReferenceException
            case NativeType.FixedSysString: _o.Write("fixed sysstring[{0}]", ((FixedSysStringMarshalInfo)marshalInfo).Size); break;

            case NativeType.IUnknown: _o.Write("iunknown"); break;

            case NativeType.IDispatch: _o.Write("idispatch"); break;

            case NativeType.Struct: _o.Write("struct"); break;

            case NativeType.IntF: _o.Write("interface"); break;

            case NativeType.SafeArray: _o.Write("safearray "); var sami = marshalInfo as SafeArrayMarshalInfo;
                if (sami != null)
                {
                    switch (sami.ElementType)
                    {
                    case VariantType.None: break;

                    case VariantType.I2: _o.Write("int16"); break;

                    case VariantType.I4: _o.Write("int32"); break;

                    case VariantType.R4: _o.Write("float32"); break;

                    case VariantType.R8: _o.Write("float64"); break;

                    case VariantType.CY: _o.Write("currency"); break;

                    case VariantType.Date: _o.Write("date"); break;

                    case VariantType.BStr: _o.Write("bstr"); break;

                    case VariantType.Dispatch: _o.Write("idispatch"); break;

                    case VariantType.Error: _o.Write("error"); break;

                    case VariantType.Bool: _o.Write("bool"); break;

                    case VariantType.Variant: _o.Write("variant"); break;

                    case VariantType.Unknown: _o.Write("iunknown"); break;

                    case VariantType.Decimal: _o.Write("decimal"); break;

                    case VariantType.I1: _o.Write("int8"); break;

                    case VariantType.UI1: _o.Write("unsigned int8"); break;

                    case VariantType.UI2: _o.Write("unsigned int16"); break;

                    case VariantType.UI4: _o.Write("unsigned int32"); break;

                    case VariantType.Int: _o.Write("int"); break;

                    case VariantType.UInt: _o.Write("unsigned int"); break;

                    default: _o.Write(sami.ElementType.ToString()); break;
                    }
                }
                break;

            case NativeType.FixedArray: _o.Write("fixed array");
                var fami = marshalInfo as FixedArrayMarshalInfo;
                if (fami != null)
                {
                    _o.Write("[{0}]", fami.Size);
                    if (fami.ElementType != NativeType.None)
                    {
                        _o.Write(' ');
                    }
                    WriteNativeType(fami.ElementType);
                }
                break;

            case NativeType.ByValStr: _o.Write("byvalstr"); break;

            case NativeType.ANSIBStr: _o.Write("ansi bstr"); break;

            case NativeType.TBStr: _o.Write("tbstr"); break;

            case NativeType.VariantBool: _o.Write("variant bool"); break;

            case NativeType.ASAny: _o.Write("as any"); break;

            case NativeType.LPStruct: _o.Write("lpstruct"); break;

            case NativeType.CustomMarshaler: var cmi = marshalInfo as CustomMarshalInfo; if (cmi == null)
                {
                    goto default;
                }
                _o.Write("custom(\"{0}\", \"{1}\"", DisassemblerHelpers.ConvertString(cmi.ManagedType.FullName), DisassemblerHelpers.ConvertString(cmi.Cookie));
                if (cmi.Guid != Guid.Empty || !string.IsNullOrEmpty(cmi.UnmanagedType))
                {
                    _o.Write(", \"{0}\", \"{1}\"", cmi.Guid.ToString(), DisassemblerHelpers.ConvertString(cmi.UnmanagedType));
                }
                _o.Write(')'); break;

            case NativeType.Error: _o.Write("error"); break;

            default: _o.Write(nativeType.ToString()); break;
            }
            //ncrunch: no coverage end
        }
Exemplo n.º 43
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="elementType">Element type</param>
		/// <param name="paramNum">Parameter number</param>
		public ArrayMarshalType(NativeType elementType, int paramNum)
			: this(elementType, paramNum, -1, -1) {
		}
Exemplo n.º 44
0
 /// <summary>
 ///     Simple fix to include the <see cref="Converter.CustomTypeHandling"/>.
 /// </summary>
 public static NativeType IncludeOverride(this NativeType @this, Converter converter, Type type)
 => converter.CustomTypeHandling.ContainsKey(type) ? converter.CustomTypeHandling[type] : @this;
Exemplo n.º 45
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="elementType">Element type</param>
		/// <param name="paramNum">Parameter number</param>
		/// <param name="numElems">Number of elements</param>
		/// <param name="flags">Flags</param>
		public ArrayMarshalType(NativeType elementType, int paramNum, int numElems, int flags)
			: base(NativeType.Array) {
			this.elementType = elementType;
			this.paramNum = paramNum;
			this.numElems = numElems;
			this.flags = flags;
		}
Exemplo n.º 46
0
        public MarshalInfo ReadMarshalInfo()
        {
            NativeType nativeType  = this.ReadNativeType();
            NativeType nativeType2 = nativeType;

            if (nativeType2 == NativeType.FixedSysString)
            {
                FixedSysStringMarshalInfo fixedSysStringMarshalInfo = new FixedSysStringMarshalInfo();
                if (this.CanReadMore())
                {
                    fixedSysStringMarshalInfo.size = (int)base.ReadCompressedUInt32();
                }
                return(fixedSysStringMarshalInfo);
            }
            switch (nativeType2)
            {
            case NativeType.SafeArray:
            {
                SafeArrayMarshalInfo safeArrayMarshalInfo = new SafeArrayMarshalInfo();
                if (this.CanReadMore())
                {
                    safeArrayMarshalInfo.element_type = this.ReadVariantType();
                }
                return(safeArrayMarshalInfo);
            }

            case NativeType.FixedArray:
            {
                FixedArrayMarshalInfo fixedArrayMarshalInfo = new FixedArrayMarshalInfo();
                if (this.CanReadMore())
                {
                    fixedArrayMarshalInfo.size = (int)base.ReadCompressedUInt32();
                }
                if (this.CanReadMore())
                {
                    fixedArrayMarshalInfo.element_type = this.ReadNativeType();
                }
                return(fixedArrayMarshalInfo);
            }

            default:
                switch (nativeType2)
                {
                case NativeType.Array:
                {
                    ArrayMarshalInfo arrayMarshalInfo = new ArrayMarshalInfo();
                    if (this.CanReadMore())
                    {
                        arrayMarshalInfo.element_type = this.ReadNativeType();
                    }
                    if (this.CanReadMore())
                    {
                        arrayMarshalInfo.size_parameter_index = (int)base.ReadCompressedUInt32();
                    }
                    if (this.CanReadMore())
                    {
                        arrayMarshalInfo.size = (int)base.ReadCompressedUInt32();
                    }
                    if (this.CanReadMore())
                    {
                        arrayMarshalInfo.size_parameter_multiplier = (int)base.ReadCompressedUInt32();
                    }
                    return(arrayMarshalInfo);
                }

                case NativeType.CustomMarshaler:
                {
                    CustomMarshalInfo customMarshalInfo = new CustomMarshalInfo();
                    string            text = this.ReadUTF8String();
                    customMarshalInfo.guid           = ((!string.IsNullOrEmpty(text)) ? new Guid(text) : Guid.Empty);
                    customMarshalInfo.unmanaged_type = this.ReadUTF8String();
                    customMarshalInfo.managed_type   = this.ReadTypeReference();
                    customMarshalInfo.cookie         = this.ReadUTF8String();
                    return(customMarshalInfo);
                }
                }
                return(new MarshalInfo(nativeType));
            }
        }
Exemplo n.º 47
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="nativeType">Native type</param>
		public InterfaceMarshalType(NativeType nativeType)
			: this(nativeType, -1) {
		}
Exemplo n.º 48
0
        void WriteNativeType(NativeType nativeType, MarshalInfo marshalInfo = null)
        {
            switch (nativeType)
            {
            case NativeType.None:
                break;

            case NativeType.Boolean:
                output.Write("bool");
                break;

            case NativeType.I1:
                output.Write("int8");
                break;

            case NativeType.U1:
                output.Write("unsigned int8");
                break;

            case NativeType.I2:
                output.Write("int16");
                break;

            case NativeType.U2:
                output.Write("unsigned int16");
                break;

            case NativeType.I4:
                output.Write("int32");
                break;

            case NativeType.U4:
                output.Write("unsigned int32");
                break;

            case NativeType.I8:
                output.Write("int64");
                break;

            case NativeType.U8:
                output.Write("unsigned int64");
                break;

            case NativeType.R4:
                output.Write("float32");
                break;

            case NativeType.R8:
                output.Write("float64");
                break;

            case NativeType.LPStr:
                output.Write("lpstr");
                break;

            case NativeType.Int:
                output.Write("int");
                break;

            case NativeType.UInt:
                output.Write("unsigned int");
                break;

            case NativeType.Func:
                goto default;                         // ??

            case NativeType.Array:
                ArrayMarshalInfo ami = (ArrayMarshalInfo)marshalInfo;
                if (ami == null)
                {
                    goto default;
                }
                if (ami.ElementType != NativeType.Max)
                {
                    WriteNativeType(ami.ElementType);
                }
                output.Write('[');
                if (ami.SizeParameterMultiplier == 0)
                {
                    output.Write(ami.Size.ToString());
                }
                else
                {
                    if (ami.Size >= 0)
                    {
                        output.Write(ami.Size.ToString());
                    }
                    output.Write(" + ");
                    output.Write(ami.SizeParameterIndex.ToString());
                }
                output.Write(']');
                break;

            case NativeType.Currency:
                output.Write("currency");
                break;

            case NativeType.BStr:
                output.Write("bstr");
                break;

            case NativeType.LPWStr:
                output.Write("lpwstr");
                break;

            case NativeType.LPTStr:
                output.Write("lptstr");
                break;

            case NativeType.FixedSysString:
                output.Write("fixed sysstring[{0}]", ((FixedSysStringMarshalInfo)marshalInfo).Size);
                break;

            case NativeType.IUnknown:
                output.Write("iunknown");
                break;

            case NativeType.IDispatch:
                output.Write("idispatch");
                break;

            case NativeType.Struct:
                output.Write("struct");
                break;

            case NativeType.IntF:
                output.Write("interface");
                break;

            case NativeType.SafeArray:
                output.Write("safearray ");
                SafeArrayMarshalInfo sami = marshalInfo as SafeArrayMarshalInfo;
                if (sami != null)
                {
                    switch (sami.ElementType)
                    {
                    case VariantType.None:
                        break;

                    case VariantType.I2:
                        output.Write("int16");
                        break;

                    case VariantType.I4:
                        output.Write("int32");
                        break;

                    case VariantType.R4:
                        output.Write("float32");
                        break;

                    case VariantType.R8:
                        output.Write("float64");
                        break;

                    case VariantType.CY:
                        output.Write("currency");
                        break;

                    case VariantType.Date:
                        output.Write("date");
                        break;

                    case VariantType.BStr:
                        output.Write("bstr");
                        break;

                    case VariantType.Dispatch:
                        output.Write("idispatch");
                        break;

                    case VariantType.Error:
                        output.Write("error");
                        break;

                    case VariantType.Bool:
                        output.Write("bool");
                        break;

                    case VariantType.Variant:
                        output.Write("variant");
                        break;

                    case VariantType.Unknown:
                        output.Write("iunknown");
                        break;

                    case VariantType.Decimal:
                        output.Write("decimal");
                        break;

                    case VariantType.I1:
                        output.Write("int8");
                        break;

                    case VariantType.UI1:
                        output.Write("unsigned int8");
                        break;

                    case VariantType.UI2:
                        output.Write("unsigned int16");
                        break;

                    case VariantType.UI4:
                        output.Write("unsigned int32");
                        break;

                    case VariantType.Int:
                        output.Write("int");
                        break;

                    case VariantType.UInt:
                        output.Write("unsigned int");
                        break;

                    default:
                        output.Write(sami.ElementType.ToString());
                        break;
                    }
                }
                break;

            case NativeType.FixedArray:
                output.Write("fixed array");
                FixedArrayMarshalInfo fami = marshalInfo as FixedArrayMarshalInfo;
                if (fami != null)
                {
                    output.Write("[{0}]", fami.Size);
                    if (fami.ElementType != NativeType.None)
                    {
                        output.Write(' ');
                        WriteNativeType(fami.ElementType);
                    }
                }
                break;

            case NativeType.ByValStr:
                output.Write("byvalstr");
                break;

            case NativeType.ANSIBStr:
                output.Write("ansi bstr");
                break;

            case NativeType.TBStr:
                output.Write("tbstr");
                break;

            case NativeType.VariantBool:
                output.Write("variant bool");
                break;

            case NativeType.ASAny:
                output.Write("as any");
                break;

            case NativeType.LPStruct:
                output.Write("lpstruct");
                break;

            case NativeType.CustomMarshaler:
                CustomMarshalInfo cmi = marshalInfo as CustomMarshalInfo;
                if (cmi == null)
                {
                    goto default;
                }
                output.Write("custom(\"{0}\", \"{1}\"",
                             NRefactory.CSharp.CSharpOutputVisitor.ConvertString(cmi.ManagedType.FullName),
                             NRefactory.CSharp.CSharpOutputVisitor.ConvertString(cmi.Cookie));
                if (cmi.Guid != Guid.Empty || !string.IsNullOrEmpty(cmi.UnmanagedType))
                {
                    output.Write(", \"{0}\", \"{1}\"", cmi.Guid.ToString(), NRefactory.CSharp.CSharpOutputVisitor.ConvertString(cmi.UnmanagedType));
                }
                output.Write(')');
                break;

            case NativeType.Error:
                output.Write("error");
                break;

            default:
                output.Write(nativeType.ToString());
                break;
            }
        }
Exemplo n.º 49
0
 public MarshalSpec(NativeType natIntr, IHasMarshalSpec container)
 {
     m_natIntr = natIntr;
     m_container = container;
 }
Exemplo n.º 50
0
 void Add(NativeType a)
 {
 }
Exemplo n.º 51
0
		public MarshalInfo (NativeType native)
		{
			this.native = native;
		}
Exemplo n.º 52
0
        internal static void EmitNumericConversion(ILGenerator ilg, NativeType fromType, NativeType toType, bool @checked)
        {
            bool fromUnsigned = fromType.IsUnsigned();

            switch (toType)
            {
            case NativeType.SByte:
                switch (fromType)
                {
                case NativeType.SByte:
                    break;         // NOP

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_I1_Un : OpCodes.Conv_Ovf_I1);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_I1);
                    }
                    break;
                }
                break;

            case NativeType.Byte:
                switch (fromType)
                {
                case NativeType.Byte:
                    break;         // NOP

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_U1_Un : OpCodes.Conv_Ovf_U1);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_U1);
                    }
                    break;
                }
                break;

            case NativeType.Int16:
                switch (fromType)
                {
                case NativeType.SByte:
                case NativeType.Byte:
                case NativeType.Int16:
                    break;         // NOP

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_I2_Un : OpCodes.Conv_Ovf_I2);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_I2);
                    }
                    break;
                }
                break;

            case NativeType.Char:
            case NativeType.UInt16:
                switch (fromType)
                {
                case NativeType.Byte:
                case NativeType.UInt16:
                case NativeType.Char:
                    break;         // NOP

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_U2_Un : OpCodes.Conv_Ovf_U2);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_U2);
                    }
                    break;
                }
                break;

            case NativeType.Int32:
                switch (fromType)
                {
                case NativeType.SByte:
                case NativeType.Byte:
                case NativeType.Int16:
                case NativeType.UInt16:
                case NativeType.Int32:
                case NativeType.Char:
                    break;         // NOP

                case NativeType.UInt32:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_I4_Un);
                    }
                    break;         // NOP in unchecked

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_I4_Un : OpCodes.Conv_Ovf_I4);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_I4);
                    }
                    break;
                }
                break;

            case NativeType.UInt32:
                switch (fromType)
                {
                case NativeType.Byte:
                case NativeType.UInt16:
                case NativeType.UInt32:
                case NativeType.Char:
                    break;         // NOP

                case NativeType.SByte:
                case NativeType.Int16:
                case NativeType.Int32:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_U4);
                    }
                    break;         // NOP in unchecked

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_U4_Un : OpCodes.Conv_Ovf_U4);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_U4);
                    }
                    break;
                }
                break;

            case NativeType.IntPtr:
                switch (fromType)
                {
                case NativeType.IntPtr:
                    break;         // NOP

                case NativeType.SByte:
                case NativeType.Int16:
                case NativeType.Int32:
                    ilg.Emit(OpCodes.Conv_I);         // potentially widening, so not NOP
                    break;

                case NativeType.Byte:
                case NativeType.UInt16:
                case NativeType.Char:
                    // Doesn't actually matter whether we sign extend, because
                    // bit 32 can't be set in any of these types.
                    ilg.Emit(OpCodes.Conv_U);         // potentially widening, so not NOP
                    break;

                case NativeType.UInt32:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_I_Un);
                    }
                    else
                    {
                        // Don't want to sign extend if this is a widening conversion.
                        ilg.Emit(OpCodes.Conv_U);         // potentially widening, so not NOP
                    }
                    break;

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_I_Un : OpCodes.Conv_Ovf_I);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_I);
                    }
                    break;
                }
                break;

            case NativeType.UIntPtr:
                switch (fromType)
                {
                case NativeType.UIntPtr:
                    break;         // NOP

                case NativeType.Byte:
                case NativeType.UInt16:
                case NativeType.UInt32:
                case NativeType.Char:
                    ilg.Emit(OpCodes.Conv_U);         // potentially widening, so not NOP
                    break;

                case NativeType.SByte:
                case NativeType.Int16:
                case NativeType.Int32:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_U);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_I);         // potentially widening, so not NOP
                    }
                    break;

                default:
                    if (@checked)
                    {
                        ilg.Emit(fromUnsigned ? OpCodes.Conv_Ovf_U_Un : OpCodes.Conv_Ovf_U);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_U);
                    }
                    break;
                }
                break;

            case NativeType.Int64:
                switch (fromType)
                {
                case NativeType.Int64:
                    break;         //NOP

                case NativeType.SByte:
                case NativeType.Int16:
                case NativeType.Int32:
                case NativeType.IntPtr:
                    ilg.Emit(OpCodes.Conv_I8);         // sign extend
                    break;

                case NativeType.Byte:
                case NativeType.UInt16:
                case NativeType.UInt32:
                case NativeType.Char:
                    ilg.Emit(OpCodes.Conv_U8);         // 0 extend
                    break;

                //case NativeType.Pointer:
                case NativeType.UIntPtr:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_I8_Un);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_U8);         // 0 extend if unchecked
                    }
                    break;

                case NativeType.UInt64:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_I8_Un);
                    }
                    break;         // NOP in unchecked

                default:
                    Debug.Assert(fromType.IsFloatingPoint());
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_I8);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_I8);
                    }
                    break;
                }
                break;

            case NativeType.UInt64:
                switch (fromType)
                {
                case NativeType.UInt64:
                    break;         //NOP

                case NativeType.Byte:
                case NativeType.UInt16:
                case NativeType.UInt32:
                //case NativeType.Pointer:
                case NativeType.UIntPtr:
                case NativeType.Char:
                    ilg.Emit(OpCodes.Conv_U8);         // 0 extend
                    break;

                case NativeType.SByte:
                case NativeType.Int16:
                case NativeType.Int32:
                case NativeType.IntPtr:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_U8);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_I8);         // sign extend if unchecked
                    }
                    break;

                case NativeType.Int64:
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_U8);
                    }
                    break;         // NOP in unchecked

                default:
                    Debug.Assert(fromType.IsFloatingPoint());
                    if (@checked)
                    {
                        ilg.Emit(OpCodes.Conv_Ovf_U8);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Conv_U8);
                    }
                    break;
                }
                break;

            case NativeType.Single:
                switch (fromType)
                {
                case NativeType.UInt32:
                case NativeType.UInt64:
                    ilg.Emit(OpCodes.Conv_R_Un);
                    break;
                }
                ilg.Emit(OpCodes.Conv_R4);
                break;

            case NativeType.Double:
                switch (fromType)
                {
                case NativeType.UInt32:
                case NativeType.UInt64:
                    ilg.Emit(OpCodes.Conv_R_Un);
                    break;
                }
                ilg.Emit(OpCodes.Conv_R8);
                break;

            /*               case NativeType.Pointer:
             *                 if (@checked)
             *                 {
             *                     switch (fromPredefTypeKind)
             *                     {
             *                         case NativeType.Byte:
             *                         case NativeType.UInt16:
             *                         case NativeType.UInt32:
             *                             ilg.Emit(OpCodes.Conv_U);
             *                             break;
             *                         case NativeType.UInt64:
             *                             ilg.Emit(OpCodes.Conv_Ovf_U_Un);
             *                             break;
             *                         case NativeType.SByte:
             *                         case NativeType.Int16:
             *                         case NativeType.Int32:
             *                         case NativeType.Int64:
             *                             ilg.Emit(OpCodes.Conv_Ovf_U);
             *                             break;
             *                         default:
             *                             throw ExceptionUtilities.UnexpectedValue(fromPredefTypeKind);
             *                     }
             *                 }
             *                 else
             *                 {
             *                     switch (fromPredefTypeKind)
             *                     {
             *                         case NativeType.Byte:
             *                         case NativeType.UInt16:
             *                         case NativeType.UInt32:
             *                         case NativeType.UInt64:
             *                         case NativeType.Int64:
             *                             ilg.Emit(OpCodes.Conv_U);
             *                             break;
             *                         case NativeType.SByte:
             *                         case NativeType.Int16:
             *                         case NativeType.Int32:
             *                             // This matches dev10.  Presumably, we're using conv_i,
             *                             // rather than conv_u, to sign-extend the value.
             *                             ilg.Emit(OpCodes.Conv_I);
             *                             break;
             *                         default:
             *                             throw ExceptionUtilities.UnexpectedValue(fromPredefTypeKind);
             *                     }
             *                 }
             *                 break;*/

            default:
                throw new InternalError();
            }
        }
Exemplo n.º 53
0
        /// <summary>
        /// Initializes the array as a <see cref="UnmanagedType.ByValArray"/>.
        /// </summary>
        private void InitializeAsByValArray(NativeTypeDesc desc)
        {
            Debug.Assert(this.indirections == 0);

            this.arrayKind = ArrayKind.ByValArray;

            // const size must be specified and >0
            if (desc.MarshalAs == null || desc.MarshalAs.SizeConst <= 0)
            {
                Log.Add(Errors.ERROR_ByValArrayInvalidLength);
                this.length = 1;
            }
            else
            {
                // no need to output any INFO as this number will be between brackets in the code
                this.length = desc.MarshalAs.SizeConst;
            }

            UnmanagedType element_unmng_type =
                (desc.MarshalAs == null ? (UnmanagedType)0 : desc.MarshalAs.ArraySubType);
            if (element_unmng_type == (UnmanagedType)80) element_unmng_type = (UnmanagedType)0;

            // determine the element type
            this.elementType = NativeType.FromClrArrayElement(desc.Type, element_unmng_type, desc.Flags);
        }
Exemplo n.º 54
0
 private void Add(NativeType a)
 {
 }
Exemplo n.º 55
0
        /// <summary>
        /// Initializes the array as a <see cref="UnmanagedType.SafeArray"/>.
        /// </summary>
        private void InitializeAsSafeArray(NativeTypeDesc desc)
        {
            this.arrayKind = ArrayKind.SafeArray;

            Type array_mng_type = desc.Type;

            VarEnum sub_type = VarEnum.VT_EMPTY;
            if (desc.MarshalAs != null && desc.MarshalAs.SafeArraySubType != VarEnum.VT_EMPTY)
            {
                sub_type = desc.MarshalAs.SafeArraySubType;
            }
            else
            {
                // the unmanaged type may also be specified statically using one of the wrapper classes
                if (array_mng_type == typeof(UnknownWrapper[]))
                {
                    array_mng_type = typeof(object[]);
                    sub_type = VarEnum.VT_UNKNOWN;
                }
                else if (array_mng_type == typeof(DispatchWrapper[]))
                {
                    array_mng_type = typeof(object[]);
                    sub_type = VarEnum.VT_DISPATCH;
                }
                else if (array_mng_type == typeof(ErrorWrapper[]))
                {
                    array_mng_type = typeof(int[]);
                    sub_type = VarEnum.VT_ERROR;
                }
                else if (array_mng_type == typeof(CurrencyWrapper[]))
                {
                    array_mng_type = typeof(Decimal[]);
                    sub_type = VarEnum.VT_CY;
                }
                else if (array_mng_type == typeof(BStrWrapper[]))
                {
                    array_mng_type = typeof(string[]);
                    sub_type = VarEnum.VT_BSTR;
                }
            }

            // convert the SafeArraySubType to UnmanagedType
            UnmanagedType element_unmng_type = Utility.VarEnumToUnmanagedType(sub_type);

            // determine the element type
            // (this will have no effect on the C++ signature but we will check it and add it to log)
            this.elementType = NativeType.FromClrArrayElement(
                array_mng_type,
                element_unmng_type,
                (desc.Flags & ~MarshalFlags.ByRefParam) | MarshalFlags.ComInterop);

            if (sub_type == VarEnum.VT_EMPTY)
            {
                sub_type = Utility.TypeToVarEnum(array_mng_type.GetElementType());
            }

            if (!elementType.IsInvalid)
            {
                if (array_mng_type != typeof(System.Array) || sub_type != VarEnum.VT_EMPTY)
                {
                    // log the element native type
                    Log.Add(Errors.INFO_SafeArrayWillMarshalAs, sub_type.ToString());
                }
            }

            // also include the *Wrapper hint if applicable
            if (desc.Type == typeof(object[]) && (sub_type == VarEnum.VT_EMPTY || sub_type == VarEnum.VT_VARIANT))
            {
                Log.Add(Errors.INFO_SafeArrayOfVariantsWrapperUse);
            }

            ExplainMemoryManagement(desc, Resources._SafeArray);
        }
Exemplo n.º 56
0
 internal static bool IsUnsigned(this NativeType t)
 {
     return(t == NativeType.Char || t == NativeType.Byte || t == NativeType.UInt16 || t == NativeType.UInt32 || t == NativeType.UInt64);
 }