示例#1
0
 /// <summary>
 /// Writes a method
 /// </summary>
 /// <param name="output">Output</param>
 /// <param name="decompiler">Decompiler</param>
 /// <param name="method">Method</param>
 /// <param name="showToken">true to write tokens</param>
 public void Write(ITextColorWriter output, IDecompiler decompiler, MethodDef method, bool showToken)
 {
     output.Write(TextColorHelper.GetColor(method), NameUtilities.CleanIdentifier(method.Name));
     output.Write(BoxedTextColor.Punctuation, "(");
     foreach (var p in method.Parameters)
     {
         if (p.IsHiddenThisParameter)
         {
             continue;
         }
         if (p.MethodSigIndex > 0)
         {
             output.WriteCommaSpace();
         }
         decompiler.WriteType(output, p.Type.ToTypeDefOrRef(), false, p.ParamDef);
     }
     if (method.CallingConvention == CallingConvention.VarArg || method.CallingConvention == CallingConvention.NativeVarArg)
     {
         if (method.MethodSig.GetParamCount() > 0)
         {
             output.WriteCommaSpace();
         }
         output.Write(BoxedTextColor.Operator, "...");
     }
     output.Write(BoxedTextColor.Punctuation, ")");
     output.WriteSpace();
     output.Write(BoxedTextColor.Punctuation, ":");
     output.WriteSpace();
     decompiler.WriteType(output, method.ReturnType.ToTypeDefOrRef(), false, method.Parameters.ReturnParameter.ParamDef);
     WriteToken(output, method, showToken);
 }
示例#2
0
 protected virtual void FormatPropertyName(IDecompilerOutput output, PropertyDef property, bool?isIndexer = null)
 {
     if (property == null)
     {
         throw new ArgumentNullException(nameof(property));
     }
     output.Write(IdentifierEscaper.Escape(property.Name), TextColorHelper.GetColor(property));
 }
示例#3
0
 protected virtual void FormatTypeName(IDecompilerOutput output, TypeDef type)
 {
     if (type == null)
     {
         throw new ArgumentNullException(nameof(type));
     }
     output.Write(IdentifierEscaper.Escape(type.Name), TextColorHelper.GetColor(type));
 }
示例#4
0
 /// <summary>
 /// Writes a field
 /// </summary>
 /// <param name="output">Output</param>
 /// <param name="decompiler">Decompiler</param>
 /// <param name="field">Field</param>
 /// <param name="showToken">true to write tokens</param>
 public void Write(ITextColorWriter output, IDecompiler decompiler, FieldDef field, bool showToken)
 {
     output.Write(TextColorHelper.GetColor(field), NameUtilities.CleanIdentifier(field.Name));
     output.WriteSpace();
     output.Write(BoxedTextColor.Punctuation, ":");
     output.WriteSpace();
     decompiler.WriteType(output, field.FieldType.ToTypeDefOrRef(), false);
     WriteToken(output, field, showToken);
 }
示例#5
0
 /// <summary>
 /// Writes an event
 /// </summary>
 /// <param name="output">Output</param>
 /// <param name="decompiler">Decompiler</param>
 /// <param name="event">Event</param>
 /// <param name="showToken">true to write tokens</param>
 public void Write(ITextColorWriter output, IDecompiler decompiler, EventDef @event, bool showToken)
 {
     output.Write(TextColorHelper.GetColor(@event), NameUtilities.CleanIdentifier(@event.Name));
     output.WriteSpace();
     output.Write(BoxedTextColor.Punctuation, ":");
     output.WriteSpace();
     decompiler.WriteType(output, @event.EventType, false);
     WriteToken(output, @event, showToken);
 }
示例#6
0
 protected virtual void TypeToString(IDecompilerOutput output, ITypeDefOrRef type, bool includeNamespace, IHasCustomAttribute typeAttributes = null)
 {
     if (type == null)
     {
         return;
     }
     if (includeNamespace)
     {
         output.Write(IdentifierEscaper.Escape(type.FullName), TextColorHelper.GetColor(type));
     }
     else
     {
         output.Write(IdentifierEscaper.Escape(type.Name), TextColorHelper.GetColor(type));
     }
 }
示例#7
0
        protected override void FormatPropertyName(IDecompilerOutput output, PropertyDef property, bool?isIndexer)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            if (!isIndexer.HasValue)
            {
                isIndexer = property.IsIndexer();
            }
            if (isIndexer.Value)
            {
                var accessor = property.GetMethod ?? property.SetMethod;
                if (accessor != null && accessor.HasOverrides)
                {
                    var methDecl      = accessor.Overrides.First().MethodDeclaration;
                    var declaringType = methDecl == null ? null : methDecl.DeclaringType;
                    TypeToString(output, declaringType, includeNamespace: true);
                    output.Write(".", BoxedTextColor.Operator);
                }
                output.Write("this", BoxedTextColor.Keyword);
                output.Write("[", BoxedTextColor.Punctuation);
                bool addSeparator = false;
                foreach (var p in property.PropertySig.GetParams())
                {
                    if (addSeparator)
                    {
                        output.Write(",", BoxedTextColor.Punctuation);
                        output.Write(" ", BoxedTextColor.Text);
                    }
                    else
                    {
                        addSeparator = true;
                    }
                    TypeToString(output, p.ToTypeDefOrRef(), includeNamespace: true);
                }
                output.Write("]", BoxedTextColor.Punctuation);
            }
            else
            {
                WriteIdentifier(output, property.Name, TextColorHelper.GetColor(property));
            }
        }
示例#8
0
        public override void Decompile(FieldDef field, IDecompilerOutput output, DecompilationContext ctx)
        {
            output.Write(IdentifierEscaper.Escape(field.FieldType.GetFullName()), field.FieldType.ToTypeDefOrRef(), DecompilerReferenceFlags.None, TextColorHelper.GetColor(field.FieldType));
            output.Write(" ", BoxedTextColor.Text);
            output.Write(IdentifierEscaper.Escape(field.Name), field, DecompilerReferenceFlags.Definition, TextColorHelper.GetColor(field));
            var c = field.Constant;

            if (c != null)
            {
                output.Write(" ", BoxedTextColor.Text);
                output.Write("=", BoxedTextColor.Operator);
                output.Write(" ", BoxedTextColor.Text);
                if (c.Value == null)
                {
                    output.Write("null", BoxedTextColor.Keyword);
                }
                else
                {
                    switch (c.Type)
                    {
                    case ElementType.Boolean:
                        if (c.Value is bool)
                        {
                            output.Write((bool)c.Value ? "true" : "false", BoxedTextColor.Keyword);
                        }
                        else
                        {
                            goto default;
                        }
                        break;

                    case ElementType.Char:
                        output.Write($"'{c.Value}'", BoxedTextColor.Char);
                        break;

                    case ElementType.I1:
                    case ElementType.U1:
                    case ElementType.I2:
                    case ElementType.U2:
                    case ElementType.I4:
                    case ElementType.U4:
                    case ElementType.I8:
                    case ElementType.U8:
                    case ElementType.R4:
                    case ElementType.R8:
                    case ElementType.I:
                    case ElementType.U:
                        output.Write($"{c.Value}", BoxedTextColor.Number);
                        break;

                    case ElementType.String:
                        output.Write($"{c.Value}", BoxedTextColor.String);
                        break;

                    default:
                        output.Write($"{c.Value}", BoxedTextColor.Text);
                        break;
                    }
                }
            }
        }
示例#9
0
 void StartKeywordBlock(IDecompilerOutput output, string keyword, IMemberDef member)
 {
     output.Write(keyword, BoxedTextColor.Keyword);
     output.Write(" ", BoxedTextColor.Text);
     output.Write(IdentifierEscaper.Escape(member.Name), member, DecompilerReferenceFlags.Definition, TextColorHelper.GetColor(member));
     output.Write(" ", BoxedTextColor.Text);
     output.Write("{", BoxedTextColor.Punctuation);
     output.WriteLine();
     output.IncreaseIndent();
 }
示例#10
0
        void CreateUI(ITextColorWriter output, object o, bool includeNamespace)
        {
            var ns = o as NamespaceSearchResult;

            if (ns != null)
            {
                output.WriteNamespace(ns.Namespace);
                return;
            }

            var td = o as TypeDef;

            if (td != null)
            {
                Debug.Assert(Context.Decompiler != null);
                Context.Decompiler.WriteType(output, td, includeNamespace);
                return;
            }

            var md = o as MethodDef;

            if (md != null)
            {
                output.Write(TextColorHelper.GetColor(md), IdentifierEscaper.Escape(md.Name));
                return;
            }

            var fd = o as FieldDef;

            if (fd != null)
            {
                output.Write(TextColorHelper.GetColor(fd), IdentifierEscaper.Escape(fd.Name));
                return;
            }

            var pd = o as PropertyDef;

            if (pd != null)
            {
                output.Write(TextColorHelper.GetColor(pd), IdentifierEscaper.Escape(pd.Name));
                return;
            }

            var ed = o as EventDef;

            if (ed != null)
            {
                output.Write(TextColorHelper.GetColor(ed), IdentifierEscaper.Escape(ed.Name));
                return;
            }

            var asm = o as AssemblyDef;

            if (asm != null)
            {
                output.Write(asm);
                return;
            }

            var mod = o as ModuleDef;

            if (mod != null)
            {
                output.WriteModule(mod.FullName);
                return;
            }

            var asmRef = o as AssemblyRef;

            if (asmRef != null)
            {
                output.Write(asmRef);
                return;
            }

            var modRef = o as ModuleRef;

            if (modRef != null)
            {
                output.WriteModule(modRef.FullName);
                return;
            }

            var paramDef = o as ParamDef;

            if (paramDef != null)
            {
                output.Write(BoxedTextColor.Parameter, IdentifierEscaper.Escape(paramDef.Name));
                return;
            }

            // non-.NET file
            var document = o as IDsDocument;

            if (document != null)
            {
                output.Write(BoxedTextColor.Text, document.GetShortName());
                return;
            }

            var resNode = o as IResourceNode;

            if (resNode != null)
            {
                output.WriteFilename(resNode.Name);
                return;
            }

            var resElNode = o as IResourceElementNode;

            if (resElNode != null)
            {
                output.WriteFilename(resElNode.Name);
                return;
            }

            var em = o as ErrorMessage;

            if (em != null)
            {
                output.Write(em.Color, em.Text);
                return;
            }

            Debug.Assert(o == null);
        }
示例#11
0
        public void Run(AstNode compilationUnit)
        {
            foreach (var en in compilationUnit.Descendants.OfType <EntityDeclaration>())
            {
                var def = en.Annotation <IMemberDef>();
                Debug.Assert(def != null);
                if (def == null)
                {
                    continue;
                }

                // The decompiler doesn't remove IteratorStateMachineAttributes/AsyncStateMachineAttribute.
                // These attributes usually contain the name of a type with invalid characters in its name
                // and will prevent the user from compiling the code.
                if (en.SymbolKind == SymbolKind.Method)
                {
                    foreach (var sect in en.Attributes)
                    {
                        foreach (var attr in sect.Attributes)
                        {
                            var ca = attr.Annotation <CustomAttribute>();
                            var fn = ca?.TypeFullName;
                            if (fn == "System.Runtime.CompilerServices.IteratorStateMachineAttribute" ||
                                fn == "System.Runtime.CompilerServices.AsyncStateMachineAttribute")
                            {
                                attr.Remove();
                            }
                        }
                        if (!sect.Attributes.Any())
                        {
                            sect.Remove();
                        }
                    }
                }

                if (makeEverythingPublic)
                {
                    const Modifiers accessFlags = Modifiers.Private | Modifiers.Internal | Modifiers.Protected | Modifiers.Public;
                    en.Modifiers = (en.Modifiers & ~accessFlags) | Modifiers.Public;

                    bool clearModifiers = false;

                    var owner = en.Parent as TypeDeclaration;
                    if (owner?.ClassType == ClassType.Enum || owner?.ClassType == ClassType.Interface)
                    {
                        clearModifiers = true;
                    }
                    else if (en is Accessor)
                    {
                        // If it's a getter/setter/adder/remover, its owner (the property/event) already is public,
                        // so remove the modifier from the accessor
                        clearModifiers = true;
                    }
                    else if (en.SymbolKind == SymbolKind.Destructor)
                    {
                        clearModifiers = true;
                    }
                    else if (en.SymbolKind == SymbolKind.Constructor && en.HasModifier(Modifiers.Static))
                    {
                        clearModifiers = true;
                    }
                    else if (en is MethodDeclaration)
                    {
                        var md = (MethodDeclaration)en;
                        if (!md.PrivateImplementationType.IsNull || (md.Parent as TypeDeclaration)?.ClassType == ClassType.Interface)
                        {
                            clearModifiers = true;
                        }
                    }
                    else if (en is CustomEventDeclaration)
                    {
                        var ed = (CustomEventDeclaration)en;
                        if (!ed.PrivateImplementationType.IsNull || (ed.Parent as TypeDeclaration)?.ClassType == ClassType.Interface)
                        {
                            clearModifiers = true;
                        }
                    }
                    else if (en is PropertyDeclaration)
                    {
                        var pd = (PropertyDeclaration)en;
                        if (!pd.PrivateImplementationType.IsNull || (pd.Parent as TypeDeclaration)?.ClassType == ClassType.Interface)
                        {
                            clearModifiers = true;
                        }
                    }

                    if (clearModifiers)
                    {
                        en.Modifiers &= ~accessFlags;
                    }
                }

                if (partialTypes.Contains(def))
                {
                    var tdecl = en as TypeDeclaration;
                    Debug.Assert(tdecl != null);
                    if (tdecl != null)
                    {
                        tdecl.Modifiers |= Modifiers.Partial;
                        if (!showDefinitions)
                        {
                            tdecl.BaseTypes.Clear();
                            tdecl.Attributes.Clear();
                        }
                    }
                }
                else
                {
                    // The decompiler doesn't support Roslyn yet so remove this since
                    // it will break compilation
                    if (def is TypeDef && def.Name == "<>c")
                    {
                        en.Remove();
                    }

                    if (showDefinitions)
                    {
                        if (!defsToShow.Contains(def))
                        {
                            en.Remove();
                        }
                    }
                    else
                    {
                        if (defsToShow.Contains(def))
                        {
                            en.Remove();
                        }
                        else if (en is CustomEventDeclaration)
                        {
                            // Convert this hidden event to an automatic event

                            var ev = (CustomEventDeclaration)en;
                            var ed = new EventDeclaration();
                            ed.ReturnType = ev.ReturnType.Detach();
                            ed.Modifiers  = ev.Modifiers;
                            ed.Variables.Add(new VariableInitializer(TextColorHelper.GetColor(ev.Annotation <EventDef>()), ev.Name));
                            ed.CopyAnnotationsFrom(ev);
                            ev.ReplaceWith(ed);
                        }
                    }
                }
            }
        }
示例#12
0
        public IEnumerable <ICSharpCode.NRefactory.VB.Ast.InterfaceMemberSpecifier> CreateMemberSpecifiersForInterfaces(IEnumerable <ICSharpCode.NRefactory.VB.Ast.AstType> interfaces)
        {
            foreach (var type in interfaces)
            {
                var def = type.Annotation <ITypeDefOrRef>().ResolveTypeDef();
                if (def == null)
                {
                    continue;
                }
                foreach (var method in def.Methods.Where(m => !m.Name.StartsWith("get_") && !m.Name.StartsWith("set_")))
                {
                    yield return(ICSharpCode.NRefactory.VB.Ast.InterfaceMemberSpecifier.CreateWithColor((ICSharpCode.NRefactory.VB.Ast.AstType)type.Clone(), method.Name, TextColorHelper.GetColor(method)));
                }

                foreach (var property in def.Properties)
                {
                    yield return(ICSharpCode.NRefactory.VB.Ast.InterfaceMemberSpecifier.CreateWithColor((ICSharpCode.NRefactory.VB.Ast.AstType)type.Clone(), property.Name, TextColorHelper.GetColor(property)));
                }
            }
        }