Ejemplo n.º 1
0
        private void WriteSecurityDeclarationArgument(CustomAttributeNamedArgument na)
        {
            var type = na.Argument.Type;

            if (type.MetadataType == MetadataType.Class || type.MetadataType == MetadataType.ValueType)
            {
                _output.Write("enum ");
                if (type.Scope != type.Module)
                {
                    _output.Write("class ");
                    _output.Write(DisassemblerHelpers.Escape(GetAssemblyQualifiedName(type)));
                }
                else
                {
                    type.WriteTo(_output, ILNameSyntax.TypeName);
                }
            }
            else
            {
                type.WriteTo(_output);
            }
            _output.Write(' ');
            _output.Write(DisassemblerHelpers.Escape(na.Name));
            _output.Write(" = ");
            if (na.Argument.Value is string value)
            {
                // secdecls use special syntax for strings
                _output.Write("string('{0}')", TextWriterTokenWriter.ConvertString(value).Replace("'", "\'"));
            }
            else
            {
                WriteConstant(na.Argument.Value);
            }
        }
Ejemplo n.º 2
0
        private 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:
                var ami = marshalInfo as ArrayMarshalInfo;
                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}]", (marshalInfo as FixedSysStringMarshalInfo)?.Size ?? 0);
                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 ");
                var 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");
                var 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:
                var cmi = marshalInfo as CustomMarshalInfo;
                if (cmi == null)
                {
                    goto default;
                }
                _output.Write("custom(\"{0}\", \"{1}\"",
                              TextWriterTokenWriter.ConvertString(cmi.ManagedType.FullName),
                              TextWriterTokenWriter.ConvertString(cmi.Cookie));
                if (cmi.Guid != Guid.Empty || !string.IsNullOrEmpty(cmi.UnmanagedType))
                {
                    _output.Write(", \"{0}\", \"{1}\"", cmi.Guid.ToString(), TextWriterTokenWriter.ConvertString(cmi.UnmanagedType));
                }
                _output.Write(')');
                break;

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

            default:
                _output.Write(nativeType.ToString());
                break;
            }
        }
Ejemplo n.º 3
0
        private void DisassembleMethodInternal(MethodDefinition method)
        {
            //    .method public hidebysig  specialname
            //               instance default class [mscorlib]System.IO.TextWriter get_BaseWriter ()  cil managed
            //

            //emit flags
            WriteEnum(method.Attributes & MethodAttributes.MemberAccessMask, _methodVisibility);
            WriteFlags(method.Attributes & ~MethodAttributes.MemberAccessMask, _methodAttributeFlags);
            if (method.IsCompilerControlled)
            {
                _output.Write("privatescope ");
            }

            if ((method.Attributes & MethodAttributes.PInvokeImpl) == MethodAttributes.PInvokeImpl)
            {
                _output.Write("pinvokeimpl");
                if (method.HasPInvokeInfo && method.PInvokeInfo != null)
                {
                    var info = method.PInvokeInfo;
                    _output.Write("(\"" + TextWriterTokenWriter.ConvertString(info.Module.Name) + "\"");

                    if (!string.IsNullOrEmpty(info.EntryPoint) && info.EntryPoint != method.Name)
                    {
                        _output.Write(" as \"" + TextWriterTokenWriter.ConvertString(info.EntryPoint) + "\"");
                    }

                    if (info.IsNoMangle)
                    {
                        _output.Write(" nomangle");
                    }

                    if (info.IsCharSetAnsi)
                    {
                        _output.Write(" ansi");
                    }
                    else if (info.IsCharSetAuto)
                    {
                        _output.Write(" autochar");
                    }
                    else if (info.IsCharSetUnicode)
                    {
                        _output.Write(" unicode");
                    }

                    if (info.SupportsLastError)
                    {
                        _output.Write(" lasterr");
                    }

                    if (info.IsCallConvCdecl)
                    {
                        _output.Write(" cdecl");
                    }
                    else if (info.IsCallConvFastcall)
                    {
                        _output.Write(" fastcall");
                    }
                    else if (info.IsCallConvStdCall)
                    {
                        _output.Write(" stdcall");
                    }
                    else if (info.IsCallConvThiscall)
                    {
                        _output.Write(" thiscall");
                    }
                    else if (info.IsCallConvWinapi)
                    {
                        _output.Write(" winapi");
                    }

                    _output.Write(')');
                }
                _output.Write(' ');
            }

            _output.WriteLine();
            _output.Indent();
            if (method.ExplicitThis)
            {
                _output.Write("instance explicit ");
            }
            else if (method.HasThis)
            {
                _output.Write("instance ");
            }

            //call convention
            // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
            WriteEnum(method.CallingConvention & (MethodCallingConvention)0x1f, _callingConvention);

            //return type
            method.ReturnType.WriteTo(_output);
            _output.Write(' ');
            if (method.MethodReturnType.HasMarshalInfo)
            {
                WriteMarshalInfo(method.MethodReturnType.MarshalInfo);
            }

            _output.Write(method.IsCompilerControlled
                ? DisassemblerHelpers.Escape(method.Name + "$PST" + method.MetadataToken.ToInt32().ToString("X8"))
                : DisassemblerHelpers.Escape(method.Name));

            WriteTypeParameters(_output, method);

            //( params )
            _output.Write(" (");
            if (method.HasParameters)
            {
                _output.WriteLine();
                _output.Indent();
                WriteParameters(method.Parameters);
                _output.Unindent();
            }
            _output.Write(") ");
            //cil managed
            WriteEnum(method.ImplAttributes & MethodImplAttributes.CodeTypeMask, _methodCodeType);
            _output.Write((method.ImplAttributes & MethodImplAttributes.ManagedMask) == MethodImplAttributes.Managed
                ? "managed "
                : "unmanaged ");
            WriteFlags(method.ImplAttributes & ~(MethodImplAttributes.CodeTypeMask | MethodImplAttributes.ManagedMask), _methodImpl);

            _output.Unindent();
            OpenBlock(defaultCollapsed: _isInType);
            WriteAttributes(method.CustomAttributes);
            if (method.HasOverrides)
            {
                foreach (var methodOverride in method.Overrides)
                {
                    _output.Write(".override method ");
                    methodOverride.WriteTo(_output);
                    _output.WriteLine();
                }
            }
            WriteParameterAttributes(0, method.MethodReturnType, method.MethodReturnType);
            foreach (var p in method.Parameters)
            {
                WriteParameterAttributes(p.Index + 1, p, p);
            }
            WriteSecurityDeclarations(method);

            if (method.HasBody)
            {
                // create IL code mappings - used in debugger
                _methodBodyDisassembler.Disassemble(method.Body);
            }

            CloseBlock("end of method " + DisassemblerHelpers.Escape(method.DeclaringType.Name) + "::" + DisassemblerHelpers.Escape(method.Name));
        }