Example #1
0
        public void ReplayTo(ICodePrinter anotherPrinter)
        {
            Debug.Assert(anotherPrinter != null && anotherPrinter != this);

            foreach (PrintEntry entry in list)
            {
                if (entry.OutputType == OutputType.End__)
                {
                    if (ReferenceEquals(entry.String, IndentMarker))
                    {
                        anotherPrinter.Indent();
                    }
                    else if (ReferenceEquals(entry.String, UnindentMarker))
                    {
                        anotherPrinter.Unindent();
                    }
                    else
                    {
                        Debug.Assert(ReferenceEquals(entry.String, NewLineMarker));
                        anotherPrinter.PrintLn();
                    }
                }
                else
                {
                    anotherPrinter.Print(entry.OutputType, entry.String);
                }
            }
        }
Example #2
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null && signature != null);

            if (hasForwardDeclaration)
            {
                printer.Print(OutputType.Keyword, "struct");
                printer.Print(OutputType.Other, " ");
                printer.PrintLn(OutputType.Identifier, Name);

                printer.Indent();
                printer.PrintLn(OutputType.Operator, "{");

                // will print "ret_type (call_conv *ptr)(params)"
                signature.PrintTo(printer, logPrinter, flags);

                printer.Unindent();
                printer.PrintLn();
                printer.Print(OutputType.Operator, "};");
            }
            else
            {
                printer.Print(OutputType.Keyword, "typedef");
                printer.Print(OutputType.Other, " ");

                // will print "ret_type (call_conv *name)(params)"
                signature.PrintTo(printer, logPrinter, flags);
            }
        }
Example #3
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null);

            base.PrintTo(printer, logPrinter, flags);
            PrintNameTo(typeName, indirections, printer, flags);
        }
Example #4
0
        /// <summary>
        /// Prints preview of all methods under the specified node to the provided printers according to the current settings.
        /// </summary>
        private void PrintMethodPreview(TreeNode node, ICodePrinter printer, ILogPrinter logPrinter, bool printDefs, bool defsFirst)
        {
            // this can take some time - try to not appear hung
            int    tick_count1     = Environment.TickCount;
            int    tick_count2     = unchecked (tick_count1 + 1);
            Cursor original_cursor = Cursor.Current;

            // this is a node representing a type - get signatures of all its methods
            ILogPrinter mem_log = new LogMemoryPrinter();
            bool        first   = true;

            try
            {
                foreach (TreeNode child in node.Nodes)
                {
                    MethodDescriptor method_descr = child.Tag as MethodDescriptor;
                    if (method_descr != null)
                    {
                        if (!first)
                        {
                            codePrinter.PrintLn();
                            codePrinter.PrintLn();
                        }
                        else
                        {
                            first = false;
                        }

                        // print no messages and no definitions
                        PrintMethod(method_descr, codePrinter, mem_log, printDefs, defsFirst);

                        int ticks = Environment.TickCount;
                        if (ticks != tick_count1 && ticks != tick_count2)
                        {
                            // it's taking too long
                            tick_count1 = Environment.TickCount;
                            tick_count2 = unchecked (tick_count1 + 1);

                            Application.DoEvents();

                            // re-set the selection on rich text box controls because DoEvents may have
                            // included some undesired user input
                            richTextBoxCode.Select(richTextBoxCode.TextLength, 0);
                            richTextBoxMessages.Select(richTextBoxMessages.TextLength, 0);

                            Cursor.Current = Cursors.WaitCursor;
                        }
                    }
                }
            }
            finally
            {
                Cursor.Current = original_cursor;
            }

            logPrinter.PrintEntry(
                Severity.Info, 0,
                "Please select an individual method in the tree to view additional information.");
        }
Example #5
0
            public PrintContext(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
            {
                Debug.Assert(printer != null && logPrinter != null);

                this.printer    = printer;
                this.logPrinter = logPrinter;
                this.flags      = flags;
            }
Example #6
0
            public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
            {
                base.PrintTo(printer, logPrinter, flags);

                printer.Print(OutputType.Keyword, (definition.isUnion ? "union" : "struct"));
                printer.Print(OutputType.Other, " ");
                printer.Print(OutputType.Identifier, Name);
                printer.Print(OutputType.Operator, ";");
            }
Example #7
0
 public override void PrintPostIdentifierTo(ICodePrinter printer, PrintFlags flags)
 {
     if (fixedLength.HasValue)
     {
         printer.Print(OutputType.Operator, "[");
         printer.Print(OutputType.Literal, fixedLength.Value.ToString());
         printer.Print(OutputType.Operator, "]");
     }
 }
Example #8
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null);

            base.PrintTo(printer, logPrinter, flags);

            switch (arrayKind)
            {
            case ArrayKind.Invalid:
            {
                // void */PVOID
                PrimitiveNativeType.PrintNameTo(TypeName.Void, 1, printer, flags);
                break;
            }

            case ArrayKind.SafeArray:
            {
                // prefix with const if in-only
                if (isInOnly)
                {
                    printer.Print(OutputType.Keyword, "const");
                    printer.Print(OutputType.Other, " ");
                }

                // SAFEARRAY * or LPSAFEARRAY
                if ((flags & PrintFlags.UsePlainC) == PrintFlags.UsePlainC)
                {
                    TypeName.PrintTo(printer, TypeName.SafeArray.PlainC);
                    printer.Print(OutputType.Other, " ");
                    printer.Print(OutputType.Operator, "*");
                }
                else
                {
                    TypeName.PrintTo(printer, "LP" + TypeName.SafeArray.WinApi);
                }
                break;
            }

            default:
            {
                elementType.PrintTo(printer, logPrinter, flags);
                break;
            }
            }

            if (indirections > 0)
            {
                // We'll suppress the [] suffix in this case; one indirection will be provided by the
                // element itself because the ByRefParam flag has been inherited by it.
                int stars = indirections;

                while (stars-- > 0)
                {
                    printer.Print(OutputType.Operator, "*");
                }
            }
        }
Example #9
0
        /// <summary>
        /// Prints one method to the provided printers according to the current settings.
        /// </summary>
        private void PrintMethod(MethodDescriptor descriptor, ICodePrinter printer, ILogPrinter logPrinter, bool printDefs, bool defsFirst)
        {
            // this is a node representing a method - get the signature (cached by the descriptor)
            SignatureGenerator.NativeSignature native_sig = descriptor.GetNativeSignature(
                aNSITargetPlatformToolStripMenuItem.Checked,
                bit64TargetPlatformToolStripMenuItem.Checked);

            PrintSignature(native_sig, printer, logPrinter, printDefs, defsFirst);
        }
Example #10
0
        public static void PrintTo(ICodePrinter printer, string typeNameStr)
        {
            // - words starting with upper-case letter are identifiers
            // - words starting with lower-case letter or underscore are keywords
            // - * is an operator
            // - space is 'other'

            StringBuilder sb   = new StringBuilder(typeNameStr.Length);
            OutputType    type = OutputType.Other;

            for (int i = 0; i < typeNameStr.Length; i++)
            {
                char ch = typeNameStr[i];

                OutputType new_type = type;
                switch (ch)
                {
                case ' ': new_type = OutputType.Other; break;

                case '*': new_type = OutputType.Operator; break;

                case '_': break;

                default:
                {
                    if (Char.IsLower(ch) && type != OutputType.TypeName)
                    {
                        new_type = OutputType.Keyword;
                    }
                    else if (Char.IsUpper(ch))
                    {
                        new_type = OutputType.TypeName;
                    }
                    break;
                }
                }

                if (new_type != type)
                {
                    if (sb.Length > 0)
                    {
                        printer.Print(type, sb.ToString());
                        sb.Length = 0;
                    }
                    type = new_type;
                }

                sb.Append(ch);
            }

            if (sb.Length > 0)
            {
                printer.Print(type, sb.ToString());
            }
        }
Example #11
0
        /// <summary>
        /// Prints one delegate to the provided printers according to the current settings.
        /// </summary>
        private void PrintDelegate(TypeDescriptor descriptor, ICodePrinter printer, ILogPrinter logPrinter, bool printDefs, bool defsFirst)
        {
            Debug.Assert(descriptor.IsDelegate);

            // this is a node representing a delegate - get the signature (cached by the descriptor)
            SignatureGenerator.NativeSignature native_sig = descriptor.GetNativeSignature(
                aNSITargetPlatformToolStripMenuItem.Checked,
                bit64TargetPlatformToolStripMenuItem.Checked);

            PrintSignature(native_sig, printer, logPrinter, printDefs, defsFirst);
        }
Example #12
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null);

            if (isUndefined && (flags & PrintFlags.UseDefinedComInterfaces) == PrintFlags.UseDefinedComInterfaces)
            {
                PrintNameTo(TypeName.IUnknown, indirections - 1, printer, flags);
            }
            else
            {
                PrintNameTo(typeName, indirections, printer, flags);
            }
        }
Example #13
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            if (printer == null) throw new ArgumentNullException(nameof(printer));
            if (logPrinter == null) throw new ArgumentNullException(nameof(logPrinter));
            Debug.Assert(fields != null);

            printer.Print(OutputType.Keyword, "enum");
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.TypeName, name);
            printer.Print(OutputType.Other, " ");

            // always explicitly print the underlying type
            printer.Print(OutputType.Operator, ":");
            printer.Print(OutputType.Other, " ");

            underlyingType.PrintTo(printer, logPrinter, flags);

            printer.PrintLn();

            printer.Indent();
            printer.Print(OutputType.Operator, "{");
            try
            {
                decimal next_value = 0;
                for (int i = 0; i < fields.Length; i++)
                {
                    // field name [ = field value ][,]
                    printer.PrintLn();

                    if (isFlagsEnum)
                    {
                        // explicitly list all field values for [Flags] enums
                        next_value = Decimal.MaxValue;
                    }
                    fields[i].PrintTo(printer, flags, ref next_value);

                    if (i < fields.Length - 1)
                    {
                        // comma after all but the last field
                        printer.Print(OutputType.Operator, ",");
                    }
                }
            }
            finally
            {
                printer.Unindent();

                printer.PrintLn();
                printer.Print(OutputType.Operator, "};");
            }
        }
Example #14
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null && fields != null);

            printer.Print(OutputType.Keyword, "enum");
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.TypeName, name);
            printer.Print(OutputType.Other, " ");

            // always explicitly print the underlying type
            printer.Print(OutputType.Operator, ":");
            printer.Print(OutputType.Other, " ");

            underlyingType.PrintTo(printer, logPrinter, flags);

            printer.PrintLn();

            printer.Indent();
            printer.Print(OutputType.Operator, "{");
            try
            {
                decimal next_value = 0;
                for (int i = 0; i < fields.Length; i++)
                {
                    // field name [ = field value ][,]
                    printer.PrintLn();

                    if (isFlagsEnum)
                    {
                        // explicitly list all field values for [Flags] enums
                        next_value = Decimal.MaxValue;
                    }
                    fields[i].PrintTo(printer, flags, ref next_value);

                    if (i < fields.Length - 1)
                    {
                        // comma after all but the last field
                        printer.Print(OutputType.Operator, ",");
                    }
                }
            }
            finally
            {
                printer.Unindent();

                printer.PrintLn();
                printer.Print(OutputType.Operator, "};");
            }
        }
Example #15
0
        /// <summary>
        /// Prints a native signature to the provided printers according to the current settings.
        /// </summary>
        private void PrintSignature(SignatureGenerator.NativeSignature nativeSig, ICodePrinter printer, ILogPrinter logPrinter,
                                    bool printDefs, bool defsFirst)
        {
            PrintFlags p_flags = PrintFlags.None;

            if (plainCTypesToolStripMenuItem.Checked)
            {
                p_flags |= PrintFlags.UsePlainC;
            }
            if (marshalDirectionAnnotationsToolStripMenuItem.Checked)
            {
                p_flags |= PrintFlags.PrintMarshalDirection;
            }

            if (!defsFirst)
            {
                nativeSig.PrintTo(printer, logPrinter, p_flags);
            }

            if (printDefs)
            {
                foreach (NativeTypeDefinition def in nativeSig.GetDefinitions())
                {
                    if (defsFirst)
                    {
                        def.PrintTo(printer, logPrinter, p_flags);
                    }

                    printer.PrintLn();
                    printer.PrintLn();

                    if (!defsFirst)
                    {
                        def.PrintTo(printer, logPrinter, p_flags);
                    }
                }
            }

            if (defsFirst)
            {
                nativeSig.PrintTo(printer, logPrinter, p_flags);
            }
        }
Example #16
0
        public MainForm(string assemblyPath, bool asDialog)
        {
            InitializeComponent();

            if (asDialog)
            {
                buttonCancel.Enabled = true;

                // make it a dialog
                this.StartPosition = FormStartPosition.CenterParent;
                this.MinimizeBox   = false;
                this.MaximizeBox   = false;
                this.ShowInTaskbar = false;

                // remove the Help menu
                menuStrip.Items.RemoveAt(menuStrip.Items.Count - 1);

                // remove the Exit menu item
                ToolStripMenuItem fileDropDown = (ToolStripMenuItem)menuStrip.Items[0];
                fileDropDown.DropDownItems.RemoveAt(fileDropDown.DropDownItems.Count - 1);
                fileDropDown.DropDownItems.RemoveAt(fileDropDown.DropDownItems.Count - 1);
            }
            else
            {
                // Remove the Ok/Cancel panel
                dialogFlowLayoutPanel.Visible = false;
            }

            treeView.PathSeparator      = "\0";
            treeView.TreeViewNodeSorter = new NodeSorter();

            codePrinter = new RichTextPrinter(this.richTextBoxCode);
            logPrinter  = new RichTextPrinter(this.richTextBoxMessages);

            if (assemblyPath != null)
            {
                LoadAssembly(assemblyPath);
            }
        }
Example #17
0
            public void PrintTo(ICodePrinter printer, PrintFlags flags, ref decimal nextValue)
            {
                if ((flags & PrintFlags.MangleEnumFields) == PrintFlags.MangleEnumFields)
                {
                    printer.Print(OutputType.Identifier, "_" + Guid.NewGuid().ToString("N") + "_");
                }
                printer.Print(OutputType.Identifier, Name);

                if (Value != nextValue)
                {
                    printer.Print(OutputType.Other, " ");
                    printer.Print(OutputType.Operator, "=");
                    printer.Print(OutputType.Other, " ");

                    printer.Print(OutputType.Literal, Value.ToString());

                    nextValue = Value;
                }
                unchecked
                {
                    nextValue++;
                }
            }
Example #18
0
        public override void PrintPostIdentifierTo(ICodePrinter printer, PrintFlags flags)
        {
            switch (arrayKind)
            {
            case ArrayKind.Invalid:
            case ArrayKind.SafeArray:
            {
                // no post identifier output
                break;
            }

            case ArrayKind.NativeArray:
            {
                if (indirections == 0)
                {
                    // We can only use the [] suffix when this array is not indirected with a byref. In
                    // that case, we use two stars prefix instead, because
                    // <element_type> (*array)[] is NOT a pointer to array!

                    // empty brackets "[]"
                    printer.Print(OutputType.Operator, "[]");
                }
                break;
            }

            case ArrayKind.ByValArray:
            {
                // length-denoting brackets "[n]"
                printer.Print(OutputType.Operator, "[");
                printer.Print(OutputType.Literal, length.ToString());
                printer.Print(OutputType.Operator, "]");
                break;
            }
            }

            base.PrintPostIdentifierTo(printer, flags);
        }
Example #19
0
        protected internal static void PrintNameTo(TypeName typeName, int indirections, ICodePrinter printer, PrintFlags flags)
        {
            if (printer == null) throw new ArgumentNullException(nameof(printer));
            string output;

            if ((flags & PrintFlags.UsePlainC) == PrintFlags.UsePlainC)
            {
                output = typeName.PlainC;
            }
            else
            {
                output = typeName.WinApi;

                if (indirections > 0)
                {
                    switch (typeName.PointerPrefix)
                    {
                        case TypeName.PtrPrefix.P_Prefix:
                        {
                            output = "P" + output;
                            indirections--;
                            break;
                        }

                        case TypeName.PtrPrefix.LP_Prefix:
                        {
                            output = "LP" + output;
                            indirections--;
                            break;
                        }
                    }
                }
            }
            TypeName.PrintTo(printer, output);

            if (indirections > 0)
            {
                if (!output.EndsWith("*")) printer.Print(OutputType.Other, " ");
                while (indirections-- > 0)
                {
                    printer.Print(OutputType.Operator, "*");
                }
            }
        }
Example #20
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            if (printer == null) throw new ArgumentNullException(nameof(printer));
            if (logPrinter == null) throw new ArgumentNullException(nameof(logPrinter));

            if (isUndefined && (flags & PrintFlags.UseDefinedComInterfaces) == PrintFlags.UseDefinedComInterfaces)
            {
                PrintNameTo(TypeName.IUnknown, indirections - 1, printer, flags);
            }
            else
            {
                PrintNameTo(typeName, indirections, printer, flags);
            }
        }
Example #21
0
        public void ReplayTo(ICodePrinter anotherPrinter)
        {
            Debug.Assert(anotherPrinter != null && anotherPrinter != this);

            foreach (PrintEntry entry in list)
            {
                if (entry.OutputType == OutputType.End__)
                {
                    if (ReferenceEquals(entry.String, IndentMarker)) anotherPrinter.Indent();
                    else if (ReferenceEquals(entry.String, UnindentMarker)) anotherPrinter.Unindent();
                    else
                    {
                        Debug.Assert(ReferenceEquals(entry.String, NewLineMarker));
                        anotherPrinter.PrintLn();
                    }
                }
                else
                    anotherPrinter.Print(entry.OutputType, entry.String);
            }
        }
Example #22
0
            public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
            {
                if (printer == null) throw new ArgumentNullException(nameof(printer));
                base.PrintTo(printer, logPrinter, flags);

                printer.Print(OutputType.Keyword, "struct");
                printer.Print(OutputType.Other, " ");
                printer.Print(OutputType.Identifier, Name);
                printer.Print(OutputType.Operator, ";");
            }
Example #23
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            if (printer == null) throw new ArgumentNullException(nameof(printer));
            if (logPrinter == null) throw new ArgumentNullException(nameof(logPrinter));
            Debug.Assert(signature != null);

            if (hasForwardDeclaration)
            {
                printer.Print(OutputType.Keyword, "struct");
                printer.Print(OutputType.Other, " ");
                printer.PrintLn(OutputType.Identifier, Name);

                printer.Indent();
                printer.PrintLn(OutputType.Operator, "{");

                // will print "ret_type (call_conv *ptr)(params)"
                signature.PrintTo(printer, logPrinter, flags);

                printer.Unindent();
                printer.PrintLn();
                printer.Print(OutputType.Operator, "};");
            }
            else
            {
                printer.Print(OutputType.Keyword, "typedef");
                printer.Print(OutputType.Other, " ");

                // will print "ret_type (call_conv *name)(params)"
                signature.PrintTo(printer, logPrinter, flags);
            }
        }
Example #24
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            if (printer == null) throw new ArgumentNullException(nameof(printer));
            printer.Print(OutputType.Identifier, "MIDL_INTERFACE");
            printer.Print(OutputType.Operator, "(");
            printer.Print(OutputType.Literal, Utility.StringToLiteral("FB6AB00F-5096-3AF8-A33D-D7885A5FA829"));
            printer.PrintLn(OutputType.Operator, ")");

            printer.Print(OutputType.TypeName, InterfaceName);
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Operator, ":");
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Keyword, "public");
            printer.Print(OutputType.Other, " ");
            printer.PrintLn(OutputType.TypeName, "IDispatch");

            printer.PrintLn(OutputType.Operator, "{");

            printer.Print(OutputType.Keyword, "public");
            printer.PrintLn(OutputType.Operator, ":");

            printer.Print(OutputType.Other, "    ");
            printer.PrintLn(OutputType.Comment, "// methods omitted");

            printer.Print(OutputType.Other, "    ");
            printer.Print(OutputType.Keyword, "virtual");
            printer.Print(OutputType.Other, " ");

            new PrimitiveNativeType(TypeName.Error, platform64bit).PrintTo(printer, logPrinter, flags);
            printer.Print(OutputType.Other, " ");

            if ((flags & PrintFlags.UsePlainC) == PrintFlags.UsePlainC)
                printer.Print(OutputType.Keyword, "__stdcall");
            else
                printer.Print(OutputType.TypeName, "STDMETHODCALLTYPE");

            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Identifier, "DynamicInvoke");
            printer.Print(OutputType.Operator, "(");

            new PrimitiveNativeType(TypeName.SafeArray, 1, platform64bit).PrintTo(printer, logPrinter, flags);
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Identifier, "args");
            printer.Print(OutputType.Operator, ",");
            printer.Print(OutputType.Other, " ");

            new PrimitiveNativeType(TypeName.Variant, 1, platform64bit).PrintTo(printer, logPrinter, flags);
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Identifier, "pRetVal");
            printer.Print(OutputType.Operator, ")");

            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Operator, "=");
            printer.Print(OutputType.Other, " ");

            printer.Print(OutputType.Literal, "0");
            printer.PrintLn(OutputType.Operator, ";");

            printer.Print(OutputType.Operator, "};");
        }
Example #25
0
        protected internal static void PrintNameTo(TypeName typeName, int indirections, ICodePrinter printer, PrintFlags flags)
        {
            string output;

            if ((flags & PrintFlags.UsePlainC) == PrintFlags.UsePlainC)
            {
                output = typeName.PlainC;
            }
            else
            {
                output = typeName.WinApi;

                if (indirections > 0)
                {
                    switch (typeName.PointerPrefix)
                    {
                    case TypeName.PtrPrefix.P_Prefix:
                    {
                        output = "P" + output;
                        indirections--;
                        break;
                    }

                    case TypeName.PtrPrefix.LP_Prefix:
                    {
                        output = "LP" + output;
                        indirections--;
                        break;
                    }
                    }
                }
            }
            TypeName.PrintTo(printer, output);

            if (indirections > 0)
            {
                if (!output.EndsWith("*"))
                {
                    printer.Print(OutputType.Other, " ");
                }
                while (indirections-- > 0)
                {
                    printer.Print(OutputType.Operator, "*");
                }
            }
        }
Example #26
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null);

            base.PrintTo(printer, logPrinter, flags);

            PrintContext context = new PrintContext(printer, logPrinter, flags);

            if (unalignedSizeOrOffsets)
            {
                // the size of the structure or field offsets is "odd" -> need to pragma pack (1) it
                context.SetPack(1, true);
            }
            else
            {
                context.SetPack(DefaultPack, true);
            }

            if (!isUnion)
            {
                printer.PrintLn();

                printer.Print(OutputType.Keyword, "struct");
                PrintIdentifierAndSize(context);

                printer.Print(OutputType.Operator, "{");

                printer.Indent();
            }

            try
            {
                if (!isInvalid)
                {
                    int current_offset = 0;

                    for (int i = 0; i < fields.Length; i++)
                    {
                        if (isExplicitLayout)
                        {
                            // this may in fact print more fields in a union, so i is passed byref
                            PrintExplicitlyLaidOutField(context, ref i, ref current_offset);
                        }
                        else
                        {
                            PrintSequentiallyLaidOutField(context, i, ref current_offset);
                        }
                    }

                    int tmp_offset = current_offset;
                    if (!context.UsedNonDefaultPack)
                    {
                        // if we never used a pack different from the default (8), we are sure
                        // about the implicit padding at the end of the structure
                        AlignSelf(ref tmp_offset);
                    }

                    if (size != tmp_offset)
                    {
                        // add final padding to the end of the structure to make its size exactly as requested
                        context.PrintPadding(size - current_offset, context.UsedNonDefaultPack);
                    }
                }
            }
            finally
            {
                if (!isUnion)
                {
                    printer.Unindent();
                    printer.PrintLn();

                    printer.Print(OutputType.Operator, "};");
                }

                context.SetDefaultPack();
            }
        }
Example #27
0
            public void PrintTo(ICodePrinter printer, PrintFlags flags, ref decimal nextValue)
            {
                if ((flags & PrintFlags.MangleEnumFields) == PrintFlags.MangleEnumFields)
                {
                    printer.Print(OutputType.Identifier, "_" + Guid.NewGuid().ToString("N") + "_");
                }
                printer.Print(OutputType.Identifier, Name);

                if (Value != nextValue)
                {
                    printer.Print(OutputType.Other, " ");
                    printer.Print(OutputType.Operator, "=");
                    printer.Print(OutputType.Other, " ");

                    printer.Print(OutputType.Literal, Value.ToString());

                    nextValue = Value;
                }
                unchecked
                {
                    nextValue++;
                }
            }
Example #28
0
        static void Main(string[] args)
        {
            options = new CommandLineOptions(args);

            // code will go to standard output
            if (options.UseColorConsoleOutput)
            {
                var console_printer = new ConsolePrinter();
                codePrinter = console_printer;
                logPrinter = console_printer; // ConsolePrinter writes log to stderr
            }
            else
            {
                codePrinter = new TextWriterPrinter(Console.Out);
                logPrinter = new TextWriterPrinter(Console.Error);
            }

            try
            {
                string assembly_path = options.AssemblyFilePath;
                Debug.Assert(!String.IsNullOrEmpty(assembly_path));

                // load the assembly
                try
                {
                    assembly = Assembly.Load(assembly_path);
                }
                catch (Exception)
                {
                    assembly = null;
                }

                if (assembly == null)
                {
                    try
                    {
                        assembly = Assembly.LoadFrom(assembly_path);
                    }
                    catch (Exception e)
                    {
                        LogLoadException(e, false, options.AssemblyFilePath);
                    }
                }

                if (assembly != null)
                {
                    Generate();
                }
            }
            finally
            {
                IDisposable disp = logPrinter as IDisposable;
                if (disp != null) disp.Dispose();

                disp = codePrinter as IDisposable;
                if (disp != null) disp.Dispose();
            }

            #if DEBUG
            // wait for Enter only if we run under VS
            if (Process.GetCurrentProcess().MainModule.ModuleName.Contains("vshost"))
            {
                Console.ReadLine();
            }
            #endif
        }
Example #29
0
        public static void PrintTo(ICodePrinter printer, string typeNameStr)
        {
            if (printer == null) throw new ArgumentNullException(nameof(printer));
            if (typeNameStr == null) throw new ArgumentNullException(nameof(typeNameStr));
            // - words starting with upper-case letter are identifiers
            // - words starting with lower-case letter or underscore are keywords
            // - * is an operator
            // - space is 'other'

            var sb = new StringBuilder(typeNameStr.Length);
            OutputType type = OutputType.Other;

            for (int i = 0; i < typeNameStr.Length; i++)
            {
                char ch = typeNameStr[i];

                OutputType new_type = type;
                switch (ch)
                {
                    case ' ': new_type = OutputType.Other; break;
                    case '*': new_type = OutputType.Operator; break;
                    case '_': break;

                    default:
                    {
                        if (Char.IsLower(ch) && type != OutputType.TypeName) new_type = OutputType.Keyword;
                        else if (Char.IsUpper(ch)) new_type = OutputType.TypeName;
                        break;
                    }
                }

                if (new_type != type)
                {
                    if (sb.Length > 0)
                    {
                        printer.Print(type, sb.ToString());
                        sb.Length = 0;
                    }
                    type = new_type;
                }

                sb.Append(ch);
            }

            if (sb.Length > 0) printer.Print(type, sb.ToString());
        }
Example #30
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null);

            if (isUndefined && (flags & PrintFlags.UseDefinedComInterfaces) == PrintFlags.UseDefinedComInterfaces)
            {
                PrintNameTo(TypeName.IUnknown, indirections - 1, printer, flags);
            }
            else
            {
                PrintNameTo(typeName, indirections, printer, flags);
            }
        }
Example #31
0
        static void Main(string[] args)
        {
            options = new CommandLineOptions(args);

            // code will go to standard output
            if (options.UseColorConsoleOutput)
            {
                ConsolePrinter console_printer = new ConsolePrinter();
                codePrinter = console_printer;
                logPrinter  = console_printer; // ConsolePrinter writes log to stderr
            }
            else
            {
                codePrinter = new TextWriterPrinter(Console.Out);
                logPrinter  = new TextWriterPrinter(Console.Error);
            }

            try
            {
                string assembly_path = options.AssemblyFilePath;
                Debug.Assert(!String.IsNullOrEmpty(assembly_path));

                // load the assembly
                try
                {
                    assembly = Assembly.Load(assembly_path);
                }
                catch (Exception)
                {
                    assembly = null;
                }

                if (assembly == null)
                {
                    try
                    {
                        assembly = Assembly.LoadFrom(assembly_path);
                    }
                    catch (Exception e)
                    {
                        LogLoadException(e, false, options.AssemblyFilePath);
                    }
                }

                if (assembly != null)
                {
                    Generate();
                }
            }
            finally
            {
                IDisposable disp = logPrinter as IDisposable;
                if (disp != null)
                {
                    disp.Dispose();
                }

                disp = codePrinter as IDisposable;
                if (disp != null)
                {
                    disp.Dispose();
                }
            }

#if DEBUG
            // wait for Enter only if we run under VS
            if (Process.GetCurrentProcess().MainModule.ModuleName.Contains("vshost"))
            {
                Console.ReadLine();
            }
#endif
        }
Example #32
0
 public override void PrintPostIdentifierTo(ICodePrinter printer, PrintFlags flags)
 {
     if (printer == null) throw new ArgumentNullException(nameof(printer));
     if (fixedLength.HasValue)
     {
         printer.Print(OutputType.Operator, "[");
         printer.Print(OutputType.Literal, fixedLength.Value.ToString());
         printer.Print(OutputType.Operator, "]");
     }
 }
Example #33
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            if (printer == null) throw new ArgumentNullException(nameof(printer));
            if (logPrinter == null) throw new ArgumentNullException(nameof(logPrinter));

            base.PrintTo(printer, logPrinter, flags);
            PrintNameTo(typeName, indirections, printer, flags);
        }
Example #34
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            printer.Print(OutputType.Identifier, "MIDL_INTERFACE");
            printer.Print(OutputType.Operator, "(");
            printer.Print(OutputType.Literal, Utility.StringToLiteral("FB6AB00F-5096-3AF8-A33D-D7885A5FA829"));
            printer.PrintLn(OutputType.Operator, ")");

            printer.Print(OutputType.TypeName, InterfaceName);
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Operator, ":");
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Keyword, "public");
            printer.Print(OutputType.Other, " ");
            printer.PrintLn(OutputType.TypeName, "IDispatch");

            printer.PrintLn(OutputType.Operator, "{");

            printer.Print(OutputType.Keyword, "public");
            printer.PrintLn(OutputType.Operator, ":");

            printer.Print(OutputType.Other, "    ");
            printer.PrintLn(OutputType.Comment, "// methods omitted");

            printer.Print(OutputType.Other, "    ");
            printer.Print(OutputType.Keyword, "virtual");
            printer.Print(OutputType.Other, " ");

            new PrimitiveNativeType(TypeName.Error, platform64bit).PrintTo(printer, logPrinter, flags);
            printer.Print(OutputType.Other, " ");

            if ((flags & PrintFlags.UsePlainC) == PrintFlags.UsePlainC)
            {
                printer.Print(OutputType.Keyword, "__stdcall");
            }
            else
            {
                printer.Print(OutputType.TypeName, "STDMETHODCALLTYPE");
            }

            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Identifier, "DynamicInvoke");
            printer.Print(OutputType.Operator, "(");

            new PrimitiveNativeType(TypeName.SafeArray, 1, platform64bit).PrintTo(printer, logPrinter, flags);
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Identifier, "args");
            printer.Print(OutputType.Operator, ",");
            printer.Print(OutputType.Other, " ");

            new PrimitiveNativeType(TypeName.Variant, 1, platform64bit).PrintTo(printer, logPrinter, flags);
            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Identifier, "pRetVal");
            printer.Print(OutputType.Operator, ")");

            printer.Print(OutputType.Other, " ");
            printer.Print(OutputType.Operator, "=");
            printer.Print(OutputType.Other, " ");

            printer.Print(OutputType.Literal, "0");
            printer.PrintLn(OutputType.Operator, ";");

            printer.Print(OutputType.Operator, "};");
        }
Example #35
0
        public override void PrintPostIdentifierTo(ICodePrinter printer, PrintFlags flags)
        {
            switch (arrayKind)
            {
                case ArrayKind.Invalid:
                case ArrayKind.SafeArray:
                {
                    // no post identifier output
                    break;
                }

                case ArrayKind.NativeArray:
                {
                    if (indirections == 0)
                    {
                        // We can only use the [] suffix when this array is not indirected with a byref. In
                        // that case, we use two stars prefix instead, because
                        // <element_type> (*array)[] is NOT a pointer to array!

                        // empty brackets "[]"
                        printer.Print(OutputType.Operator, "[]");
                    }
                    break;
                }

                case ArrayKind.ByValArray:
                {
                    // length-denoting brackets "[n]"
                    printer.Print(OutputType.Operator, "[");
                    printer.Print(OutputType.Literal, length.ToString());
                    printer.Print(OutputType.Operator, "]");
                    break;
                }
            }

            base.PrintPostIdentifierTo(printer, flags);
        }
Example #36
0
        public override void PrintTo(ICodePrinter printer, ILogPrinter logPrinter, PrintFlags flags)
        {
            Debug.Assert(printer != null && logPrinter != null);

            base.PrintTo(printer, logPrinter, flags);

            switch (arrayKind)
            {
                case ArrayKind.Invalid:
                {
                    // void */PVOID
                    PrimitiveNativeType.PrintNameTo(TypeName.Void, 1, printer, flags);
                    break;
                }

                case ArrayKind.SafeArray:
                {
                    // prefix with const if in-only
                    if (isInOnly)
                    {
                        printer.Print(OutputType.Keyword, "const");
                        printer.Print(OutputType.Other, " ");
                    }

                    // SAFEARRAY * or LPSAFEARRAY
                    if ((flags & PrintFlags.UsePlainC) == PrintFlags.UsePlainC)
                    {
                        TypeName.PrintTo(printer, TypeName.SafeArray.PlainC);
                        printer.Print(OutputType.Other, " ");
                        printer.Print(OutputType.Operator, "*");
                    }
                    else
                    {
                        TypeName.PrintTo(printer, "LP" + TypeName.SafeArray.WinApi);
                    }
                    break;
                }

                default:
                {
                    elementType.PrintTo(printer, logPrinter, flags);
                    break;
                }
            }

            if (indirections > 0)
            {
                // We'll suppress the [] suffix in this case; one indirection will be provided by the
                // element itself because the ByRefParam flag has been inherited by it.
                int stars = indirections;

                while (stars-- > 0) printer.Print(OutputType.Operator, "*");
            }
        }
Example #37
0
 public override void PrintPostIdentifierTo(ICodePrinter printer, PrintFlags flags)
 {
     if (fixedLength.HasValue)
     {
         printer.Print(OutputType.Operator, "[");
         printer.Print(OutputType.Literal, fixedLength.Value.ToString());
         printer.Print(OutputType.Operator, "]");
     }
 }