Ejemplo n.º 1
0
        /// <summary>
        /// Generate the specified union
        /// </summary>
        /// <param name="ntUnion"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public CodeTypeDeclaration GenerateUnion(NativeUnion ntUnion)
        {
            if (ntUnion == null)
            {
                throw new ArgumentNullException("ntUnion");
            }

            // Generate the core type
            CodeTypeDeclaration ctd = new CodeTypeDeclaration(ntUnion.Name);

            ctd.IsStruct = true;
            ctd.UserData[TransformConstants.DefinedType] = ntUnion;

            // Add the struct layout attribute
            ctd.CustomAttributes.Add(MarshalAttributeFactory.CreateStructLayoutAttribute(LayoutKind.Explicit));

            // Generate the container members
            GenerateContainerMembers(ntUnion, ctd);

            // Go through and put each struct back at the start of the struct to simulate the
            // union
            foreach (CodeTypeMember member in ctd.Members)
            {
                CodeMemberField fieldMember = member as CodeMemberField;
                if (fieldMember != null)
                {
                    fieldMember.CustomAttributes.Add(MarshalAttributeFactory.CreateFieldOffsetAttribute(0));
                }
            }

            return(ctd);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate a delegate in code
        /// </summary>
        /// <param name="ntFuncPtr"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public CodeTypeDelegate GenerateDelegate(NativeFunctionPointer ntFuncPtr)
        {
            if (ntFuncPtr == null)
            {
                throw new ArgumentNullException("ntFuncPtr");
            }

            string           comment = "Return Type: ";
            CodeTypeDelegate del     = new CodeTypeDelegate();

            del.Name       = ntFuncPtr.Name;
            del.Attributes = MemberAttributes.Public;
            del.ReturnType = GenerateTypeReferenceImpl(ntFuncPtr.Signature.ReturnType, ref comment);
            del.Parameters.AddRange(GenerateParameters(ntFuncPtr.Signature, ref comment));

            // If there is a non-default calling convention we need to generate the attribute
            if (ntFuncPtr.CallingConvention == NativeCallingConvention.CDeclaration || ntFuncPtr.CallingConvention == NativeCallingConvention.Standard)
            {
                del.CustomAttributes.Add(MarshalAttributeFactory.CreateUnmanagedFunctionPointerAttribute(ntFuncPtr.CallingConvention));
            }

            del.UserData[TransformConstants.DefinedType]   = ntFuncPtr;
            del.UserData[TransformConstants.ReturnType]    = ntFuncPtr.Signature.ReturnType;
            del.UserData[TransformConstants.ReturnTypeSal] = ntFuncPtr.Signature.ReturnTypeSalAttribute;
            del.Comments.Add(new CodeCommentStatement(comment, true));

            return(del);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generate a procedure from the specified proc
        /// </summary>
        /// <param name="ntProc"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public CodeMemberMethod GenerateProcedure(NativeProcedure ntProc)
        {
            if (ntProc == null)
            {
                throw new ArgumentNullException("ntProc");
            }

            // Create the proc
            NativeSignature  ntSig       = ntProc.Signature;
            string           procComment = "Return Type: ";
            CodeMemberMethod proc        = new CodeMemberMethod();

            proc.Name       = ntProc.Name;
            proc.ReturnType = GenerateTypeReferenceImpl(ntSig.ReturnType, ref procComment);
            proc.UserData[TransformConstants.ReturnType] = ntSig.ReturnType;
            if (ntSig.ReturnTypeSalAttribute != null)
            {
                proc.UserData[TransformConstants.ReturnTypeSal] = ntSig.ReturnTypeSalAttribute;
            }
            else
            {
                proc.UserData[TransformConstants.ReturnTypeSal] = new NativeSalAttribute();
            }
            proc.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            proc.UserData[TransformConstants.Procedure]     = ntProc;
            proc.UserData[TransformConstants.ReturnType]    = ntSig.ReturnType;
            proc.UserData[TransformConstants.ReturnTypeSal] = ntSig.ReturnTypeSalAttribute;

            // Add the DLL import attribute
            string dllName = ntProc.DllName;

            if (string.IsNullOrEmpty(dllName))
            {
                dllName = "<Unknown>";
            }
            proc.CustomAttributes.Add(MarshalAttributeFactory.CreateDllImportAttribute(dllName, ntProc.Name, ntProc.CallingConvention));

            // Generate the parameters
            proc.Parameters.AddRange(GenerateParameters(ntProc.Signature, ref procComment));
            proc.Comments.Add(new CodeCommentStatement(procComment, true));
            return(proc);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generate the struct definition
        /// </summary>
        /// <param name="ntStruct"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public CodeTypeDeclaration GenerateStruct(NativeStruct ntStruct)
        {
            if (ntStruct == null)
            {
                throw new ArgumentNullException("ntStruct");
            }

            // Generate the core type
            CodeTypeDeclaration ctd = new CodeTypeDeclaration(ntStruct.Name);

            ctd.IsStruct = true;
            ctd.UserData[TransformConstants.DefinedType] = ntStruct;

            // Add the struct layout attribute
            ctd.CustomAttributes.Add(MarshalAttributeFactory.CreateStructLayoutAttribute(LayoutKind.Sequential));

            GenerateContainerMembers(ntStruct, ctd);

            return(ctd);
        }