diff options
author | Stefano Angeleri <weltall2@gmail.com> | 2013-06-11 10:33:49 +0400 |
---|---|---|
committer | Stefano Angeleri <weltall2@gmail.com> | 2013-06-11 10:33:49 +0400 |
commit | ddef929dc5da43cb94e7962476828d8e02f4e389 (patch) | |
tree | 39931ecb4fc1d514a0b7bdee82de539c66379b88 /Mono.Addins.CecilReflector | |
parent | 482d38f9a8365ef039700c0b1cbe0882dfad2349 (diff) |
Updated Cecil from upstream.
Diffstat (limited to 'Mono.Addins.CecilReflector')
126 files changed, 1862 insertions, 1121 deletions
diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Code.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Code.cs index bd18b84..fa88b63 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Code.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Code.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeReader.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeReader.cs index 4cfc7db..12ca5a2 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeReader.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeReader.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -49,18 +49,13 @@ namespace Mono.Cecil.Cil { get { return base.position - start; } } - CodeReader (Section section, MetadataReader reader) + public CodeReader (Section section, MetadataReader reader) : base (section.Data) { this.code_section = section; this.reader = reader; } - public static CodeReader CreateCodeReader (MetadataReader metadata) - { - return new CodeReader (metadata.image.MetadataSection, metadata); - } - public MethodBody ReadMethodBody (MethodDefinition method) { this.method = method; @@ -107,7 +102,7 @@ namespace Mono.Cecil.Cil { throw new InvalidOperationException (); } - var symbol_reader = reader.module.SymbolReader; + var symbol_reader = reader.module.symbol_reader; if (symbol_reader != null) { var instructions = body.Instructions; @@ -150,7 +145,7 @@ namespace Mono.Cecil.Cil { code_size = 0; var end = start + code_size; - var instructions = body.instructions = new InstructionCollection (code_size / 3); + var instructions = body.instructions = new InstructionCollection ((code_size + 1) / 2); while (position < end) { var offset = base.position - start; @@ -364,7 +359,6 @@ namespace Mono.Cecil.Cil { break; case ExceptionHandlerType.Filter: handler.FilterStart = GetInstruction (ReadInt32 ()); - handler.FilterEnd = handler.HandlerStart.Previous; break; default: Advance (4); @@ -415,7 +409,7 @@ namespace Mono.Cecil.Cil { throw new NotSupportedException (); } - var symbol_reader = reader.module.SymbolReader; + var symbol_reader = reader.module.symbol_reader; if (symbol_reader != null && writer.metadata.write_symbols) { symbols.method_token = GetOriginalToken (writer.metadata, method); symbols.local_var_token = local_var_token; diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs index 8bf0722..ac093ca 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -424,10 +424,18 @@ namespace Mono.Cecil.Cil { switch (instruction.opcode.FlowControl) { case FlowControl.Call: { var method = (IMethodSignature) instruction.operand; - stack_size -= (method.HasParameters ? method.Parameters.Count : 0) - + (method.HasThis && instruction.opcode.Code != Code.Newobj ? 1 : 0); - stack_size += (method.ReturnType.etype == ElementType.Void ? 0 : 1) - + (method.HasThis && instruction.opcode.Code == Code.Newobj ? 1 : 0); + // pop 'this' argument + if (method.HasImplicitThis() && instruction.opcode.Code != Code.Newobj) + stack_size--; + // pop normal arguments + if (method.HasParameters) + stack_size -= method.Parameters.Count; + // pop function pointer + if (instruction.opcode.Code == Code.Calli) + stack_size--; + // push return value + if (method.ReturnType.etype != ElementType.Void || instruction.opcode.Code == Code.Newobj) + stack_size++; break; } default: @@ -510,7 +518,7 @@ namespace Mono.Cecil.Cil { return true; if (handler.HandlerType == ExceptionHandlerType.Filter - && IsFatRange (handler.FilterStart, handler.FilterEnd)) + && IsFatRange (handler.FilterStart, handler.HandlerStart)) return true; } @@ -519,6 +527,9 @@ namespace Mono.Cecil.Cil { static bool IsFatRange (Instruction start, Instruction end) { + if (start == null) + throw new ArgumentException (); + if (end == null) return true; diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Document.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Document.cs index e46d2c1..1dbdc44 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Document.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Document.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -54,6 +54,7 @@ namespace Mono.Cecil.Cil { JScript, Smc, MCpp, + FSharp, } public enum DocumentLanguageVendor { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ExceptionHandler.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ExceptionHandler.cs index a76f8f3..c61ff23 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ExceptionHandler.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ExceptionHandler.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -40,7 +40,6 @@ namespace Mono.Cecil.Cil { Instruction try_start; Instruction try_end; Instruction filter_start; - Instruction filter_end; Instruction handler_start; Instruction handler_end; @@ -62,11 +61,6 @@ namespace Mono.Cecil.Cil { set { filter_start = value; } } - public Instruction FilterEnd { - get { return filter_end; } - set { filter_end = value; } - } - public Instruction HandlerStart { get { return handler_start; } set { handler_start = value; } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ILProcessor.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ILProcessor.cs index 1300b3e..cd5dbb4 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ILProcessor.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/ILProcessor.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Instruction.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Instruction.cs index e8d36c4..c28d4c9 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Instruction.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Instruction.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs index b57900e..3eefc95 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -100,7 +100,18 @@ namespace Mono.Cecil.Cil { if (method == null || method.DeclaringType == null) throw new NotSupportedException (); - return this_parameter ?? (this_parameter = new ParameterDefinition ("0", ParameterAttributes.None, method.DeclaringType)); + if (!method.HasThis) + return null; + + if (this_parameter != null) + return this_parameter; + + var declaring_type = method.DeclaringType; + var type = declaring_type.IsValueType || declaring_type.IsPrimitive + ? new PointerType (declaring_type) + : declaring_type as TypeReference; + + return this_parameter = new ParameterDefinition (type, method); } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCode.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCode.cs index aad4ba4..1a14421 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCode.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCode.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -116,7 +116,7 @@ namespace Mono.Cecil.Cil { readonly byte stack_behavior_push; public string Name { - get { return OpCodeNames.names [op1 == 0xff ? op2 : op2 + 256]; } + get { return OpCodeNames.names [(int) Code]; } } public int Size { @@ -132,7 +132,7 @@ namespace Mono.Cecil.Cil { } public short Value { - get { return (short) ((op1 << 8) | op2); } + get { return op1 == 0xff ? op2 : (short) ((op1 << 8) | op2); } } public Code Code { @@ -214,311 +214,242 @@ namespace Mono.Cecil.Cil { static class OpCodeNames { - internal static readonly string [] names = { - "nop", - "break", - "ldarg.0", - "ldarg.1", - "ldarg.2", - "ldarg.3", - "ldloc.0", - "ldloc.1", - "ldloc.2", - "ldloc.3", - "stloc.0", - "stloc.1", - "stloc.2", - "stloc.3", - "ldarg.s", - "ldarga.s", - "starg.s", - "ldloc.s", - "ldloca.s", - "stloc.s", - "ldnull", - "ldc.i4.m1", - "ldc.i4.0", - "ldc.i4.1", - "ldc.i4.2", - "ldc.i4.3", - "ldc.i4.4", - "ldc.i4.5", - "ldc.i4.6", - "ldc.i4.7", - "ldc.i4.8", - "ldc.i4.s", - "ldc.i4", - "ldc.i8", - "ldc.r4", - "ldc.r8", - null, - "dup", - "pop", - "jmp", - "call", - "calli", - "ret", - "br.s", - "brfalse.s", - "brtrue.s", - "beq.s", - "bge.s", - "bgt.s", - "ble.s", - "blt.s", - "bne.un.s", - "bge.un.s", - "bgt.un.s", - "ble.un.s", - "blt.un.s", - "br", - "brfalse", - "brtrue", - "beq", - "bge", - "bgt", - "ble", - "blt", - "bne.un", - "bge.un", - "bgt.un", - "ble.un", - "blt.un", - "switch", - "ldind.i1", - "ldind.u1", - "ldind.i2", - "ldind.u2", - "ldind.i4", - "ldind.u4", - "ldind.i8", - "ldind.i", - "ldind.r4", - "ldind.r8", - "ldind.ref", - "stind.ref", - "stind.i1", - "stind.i2", - "stind.i4", - "stind.i8", - "stind.r4", - "stind.r8", - "add", - "sub", - "mul", - "div", - "div.un", - "rem", - "rem.un", - "and", - "or", - "xor", - "shl", - "shr", - "shr.un", - "neg", - "not", - "conv.i1", - "conv.i2", - "conv.i4", - "conv.i8", - "conv.r4", - "conv.r8", - "conv.u4", - "conv.u8", - "callvirt", - "cpobj", - "ldobj", - "ldstr", - "newobj", - "castclass", - "isinst", - "conv.r.un", - null, - null, - "unbox", - "throw", - "ldfld", - "ldflda", - "stfld", - "ldsfld", - "ldsflda", - "stsfld", - "stobj", - "conv.ovf.i1.un", - "conv.ovf.i2.un", - "conv.ovf.i4.un", - "conv.ovf.i8.un", - "conv.ovf.u1.un", - "conv.ovf.u2.un", - "conv.ovf.u4.un", - "conv.ovf.u8.un", - "conv.ovf.i.un", - "conv.ovf.u.un", - "box", - "newarr", - "ldlen", - "ldelema", - "ldelem.i1", - "ldelem.u1", - "ldelem.i2", - "ldelem.u2", - "ldelem.i4", - "ldelem.u4", - "ldelem.i8", - "ldelem.i", - "ldelem.r4", - "ldelem.r8", - "ldelem.ref", - "stelem.i", - "stelem.i1", - "stelem.i2", - "stelem.i4", - "stelem.i8", - "stelem.r4", - "stelem.r8", - "stelem.ref", - "ldelem.any", - "stelem.any", - "unbox.any", - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - "conv.ovf.i1", - "conv.ovf.u1", - "conv.ovf.i2", - "conv.ovf.u2", - "conv.ovf.i4", - "conv.ovf.u4", - "conv.ovf.i8", - "conv.ovf.u8", - null, - null, - null, - null, - null, - null, - null, - "refanyval", - "ckfinite", - null, - null, - "mkrefany", - null, - null, - null, - null, - null, - null, - null, - null, - null, - "ldtoken", - "conv.u2", - "conv.u1", - "conv.i", - "conv.ovf.i", - "conv.ovf.u", - "add.ovf", - "add.ovf.un", - "mul.ovf", - "mul.ovf.un", - "sub.ovf", - "sub.ovf.un", - "endfinally", - "leave", - "leave.s", - "stind.i", - "conv.u", - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - "prefix7", - "prefix6", - "prefix5", - "prefix4", - "prefix3", - "prefix2", - "prefix1", - "prefixref", - "arglist", - "ceq", - "cgt", - "cgt.un", - "clt", - "clt.un", - "ldftn", - "ldvirtftn", - null, - "ldarg", - "ldarga", - "starg", - "ldloc", - "ldloca", - "stloc", - "localloc", - null, - "endfilter", - "unaligned.", - "volatile.", - "tail.", - "initobj", - "constrained.", - "cpblk", - "initblk", - "no.", // added by spouliot to match Cecil existing definitions - "rethrow", - null, - "sizeof", - "refanytype", - "readonly.", // added by spouliot to match Cecil existing definitions - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null, - }; + internal static readonly string [] names; + + static OpCodeNames () + { + var table = new byte [] { + 3, 110, 111, 112, + 5, 98, 114, 101, 97, 107, + 7, 108, 100, 97, 114, 103, 46, 48, + 7, 108, 100, 97, 114, 103, 46, 49, + 7, 108, 100, 97, 114, 103, 46, 50, + 7, 108, 100, 97, 114, 103, 46, 51, + 7, 108, 100, 108, 111, 99, 46, 48, + 7, 108, 100, 108, 111, 99, 46, 49, + 7, 108, 100, 108, 111, 99, 46, 50, + 7, 108, 100, 108, 111, 99, 46, 51, + 7, 115, 116, 108, 111, 99, 46, 48, + 7, 115, 116, 108, 111, 99, 46, 49, + 7, 115, 116, 108, 111, 99, 46, 50, + 7, 115, 116, 108, 111, 99, 46, 51, + 7, 108, 100, 97, 114, 103, 46, 115, + 8, 108, 100, 97, 114, 103, 97, 46, 115, + 7, 115, 116, 97, 114, 103, 46, 115, + 7, 108, 100, 108, 111, 99, 46, 115, + 8, 108, 100, 108, 111, 99, 97, 46, 115, + 7, 115, 116, 108, 111, 99, 46, 115, + 6, 108, 100, 110, 117, 108, 108, + 9, 108, 100, 99, 46, 105, 52, 46, 109, 49, + 8, 108, 100, 99, 46, 105, 52, 46, 48, + 8, 108, 100, 99, 46, 105, 52, 46, 49, + 8, 108, 100, 99, 46, 105, 52, 46, 50, + 8, 108, 100, 99, 46, 105, 52, 46, 51, + 8, 108, 100, 99, 46, 105, 52, 46, 52, + 8, 108, 100, 99, 46, 105, 52, 46, 53, + 8, 108, 100, 99, 46, 105, 52, 46, 54, + 8, 108, 100, 99, 46, 105, 52, 46, 55, + 8, 108, 100, 99, 46, 105, 52, 46, 56, + 8, 108, 100, 99, 46, 105, 52, 46, 115, + 6, 108, 100, 99, 46, 105, 52, + 6, 108, 100, 99, 46, 105, 56, + 6, 108, 100, 99, 46, 114, 52, + 6, 108, 100, 99, 46, 114, 56, + 3, 100, 117, 112, + 3, 112, 111, 112, + 3, 106, 109, 112, + 4, 99, 97, 108, 108, + 5, 99, 97, 108, 108, 105, + 3, 114, 101, 116, + 4, 98, 114, 46, 115, + 9, 98, 114, 102, 97, 108, 115, 101, 46, 115, + 8, 98, 114, 116, 114, 117, 101, 46, 115, + 5, 98, 101, 113, 46, 115, + 5, 98, 103, 101, 46, 115, + 5, 98, 103, 116, 46, 115, + 5, 98, 108, 101, 46, 115, + 5, 98, 108, 116, 46, 115, + 8, 98, 110, 101, 46, 117, 110, 46, 115, + 8, 98, 103, 101, 46, 117, 110, 46, 115, + 8, 98, 103, 116, 46, 117, 110, 46, 115, + 8, 98, 108, 101, 46, 117, 110, 46, 115, + 8, 98, 108, 116, 46, 117, 110, 46, 115, + 2, 98, 114, + 7, 98, 114, 102, 97, 108, 115, 101, + 6, 98, 114, 116, 114, 117, 101, + 3, 98, 101, 113, + 3, 98, 103, 101, + 3, 98, 103, 116, + 3, 98, 108, 101, + 3, 98, 108, 116, + 6, 98, 110, 101, 46, 117, 110, + 6, 98, 103, 101, 46, 117, 110, + 6, 98, 103, 116, 46, 117, 110, + 6, 98, 108, 101, 46, 117, 110, + 6, 98, 108, 116, 46, 117, 110, + 6, 115, 119, 105, 116, 99, 104, + 8, 108, 100, 105, 110, 100, 46, 105, 49, + 8, 108, 100, 105, 110, 100, 46, 117, 49, + 8, 108, 100, 105, 110, 100, 46, 105, 50, + 8, 108, 100, 105, 110, 100, 46, 117, 50, + 8, 108, 100, 105, 110, 100, 46, 105, 52, + 8, 108, 100, 105, 110, 100, 46, 117, 52, + 8, 108, 100, 105, 110, 100, 46, 105, 56, + 7, 108, 100, 105, 110, 100, 46, 105, + 8, 108, 100, 105, 110, 100, 46, 114, 52, + 8, 108, 100, 105, 110, 100, 46, 114, 56, + 9, 108, 100, 105, 110, 100, 46, 114, 101, 102, + 9, 115, 116, 105, 110, 100, 46, 114, 101, 102, + 8, 115, 116, 105, 110, 100, 46, 105, 49, + 8, 115, 116, 105, 110, 100, 46, 105, 50, + 8, 115, 116, 105, 110, 100, 46, 105, 52, + 8, 115, 116, 105, 110, 100, 46, 105, 56, + 8, 115, 116, 105, 110, 100, 46, 114, 52, + 8, 115, 116, 105, 110, 100, 46, 114, 56, + 3, 97, 100, 100, + 3, 115, 117, 98, + 3, 109, 117, 108, + 3, 100, 105, 118, + 6, 100, 105, 118, 46, 117, 110, + 3, 114, 101, 109, + 6, 114, 101, 109, 46, 117, 110, + 3, 97, 110, 100, + 2, 111, 114, + 3, 120, 111, 114, + 3, 115, 104, 108, + 3, 115, 104, 114, + 6, 115, 104, 114, 46, 117, 110, + 3, 110, 101, 103, + 3, 110, 111, 116, + 7, 99, 111, 110, 118, 46, 105, 49, + 7, 99, 111, 110, 118, 46, 105, 50, + 7, 99, 111, 110, 118, 46, 105, 52, + 7, 99, 111, 110, 118, 46, 105, 56, + 7, 99, 111, 110, 118, 46, 114, 52, + 7, 99, 111, 110, 118, 46, 114, 56, + 7, 99, 111, 110, 118, 46, 117, 52, + 7, 99, 111, 110, 118, 46, 117, 56, + 8, 99, 97, 108, 108, 118, 105, 114, 116, + 5, 99, 112, 111, 98, 106, + 5, 108, 100, 111, 98, 106, + 5, 108, 100, 115, 116, 114, + 6, 110, 101, 119, 111, 98, 106, + 9, 99, 97, 115, 116, 99, 108, 97, 115, 115, + 6, 105, 115, 105, 110, 115, 116, + 9, 99, 111, 110, 118, 46, 114, 46, 117, 110, + 5, 117, 110, 98, 111, 120, + 5, 116, 104, 114, 111, 119, + 5, 108, 100, 102, 108, 100, + 6, 108, 100, 102, 108, 100, 97, + 5, 115, 116, 102, 108, 100, + 6, 108, 100, 115, 102, 108, 100, + 7, 108, 100, 115, 102, 108, 100, 97, + 6, 115, 116, 115, 102, 108, 100, + 5, 115, 116, 111, 98, 106, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49, 46, 117, 110, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50, 46, 117, 110, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52, 46, 117, 110, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56, 46, 117, 110, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49, 46, 117, 110, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50, 46, 117, 110, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52, 46, 117, 110, + 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56, 46, 117, 110, + 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 46, 117, 110, + 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 46, 117, 110, + 3, 98, 111, 120, + 6, 110, 101, 119, 97, 114, 114, + 5, 108, 100, 108, 101, 110, + 7, 108, 100, 101, 108, 101, 109, 97, + 9, 108, 100, 101, 108, 101, 109, 46, 105, 49, + 9, 108, 100, 101, 108, 101, 109, 46, 117, 49, + 9, 108, 100, 101, 108, 101, 109, 46, 105, 50, + 9, 108, 100, 101, 108, 101, 109, 46, 117, 50, + 9, 108, 100, 101, 108, 101, 109, 46, 105, 52, + 9, 108, 100, 101, 108, 101, 109, 46, 117, 52, + 9, 108, 100, 101, 108, 101, 109, 46, 105, 56, + 8, 108, 100, 101, 108, 101, 109, 46, 105, + 9, 108, 100, 101, 108, 101, 109, 46, 114, 52, + 9, 108, 100, 101, 108, 101, 109, 46, 114, 56, + 10, 108, 100, 101, 108, 101, 109, 46, 114, 101, 102, + 8, 115, 116, 101, 108, 101, 109, 46, 105, + 9, 115, 116, 101, 108, 101, 109, 46, 105, 49, + 9, 115, 116, 101, 108, 101, 109, 46, 105, 50, + 9, 115, 116, 101, 108, 101, 109, 46, 105, 52, + 9, 115, 116, 101, 108, 101, 109, 46, 105, 56, + 9, 115, 116, 101, 108, 101, 109, 46, 114, 52, + 9, 115, 116, 101, 108, 101, 109, 46, 114, 56, + 10, 115, 116, 101, 108, 101, 109, 46, 114, 101, 102, + 10, 108, 100, 101, 108, 101, 109, 46, 97, 110, 121, + 10, 115, 116, 101, 108, 101, 109, 46, 97, 110, 121, + 9, 117, 110, 98, 111, 120, 46, 97, 110, 121, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56, + 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56, + 9, 114, 101, 102, 97, 110, 121, 118, 97, 108, + 8, 99, 107, 102, 105, 110, 105, 116, 101, + 8, 109, 107, 114, 101, 102, 97, 110, 121, + 7, 108, 100, 116, 111, 107, 101, 110, + 7, 99, 111, 110, 118, 46, 117, 50, + 7, 99, 111, 110, 118, 46, 117, 49, + 6, 99, 111, 110, 118, 46, 105, + 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, + 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, + 7, 97, 100, 100, 46, 111, 118, 102, + 10, 97, 100, 100, 46, 111, 118, 102, 46, 117, 110, + 7, 109, 117, 108, 46, 111, 118, 102, + 10, 109, 117, 108, 46, 111, 118, 102, 46, 117, 110, + 7, 115, 117, 98, 46, 111, 118, 102, + 10, 115, 117, 98, 46, 111, 118, 102, 46, 117, 110, + 10, 101, 110, 100, 102, 105, 110, 97, 108, 108, 121, + 5, 108, 101, 97, 118, 101, + 7, 108, 101, 97, 118, 101, 46, 115, + 7, 115, 116, 105, 110, 100, 46, 105, + 6, 99, 111, 110, 118, 46, 117, + 7, 97, 114, 103, 108, 105, 115, 116, + 3, 99, 101, 113, + 3, 99, 103, 116, + 6, 99, 103, 116, 46, 117, 110, + 3, 99, 108, 116, + 6, 99, 108, 116, 46, 117, 110, + 5, 108, 100, 102, 116, 110, + 9, 108, 100, 118, 105, 114, 116, 102, 116, 110, + 5, 108, 100, 97, 114, 103, + 6, 108, 100, 97, 114, 103, 97, + 5, 115, 116, 97, 114, 103, + 5, 108, 100, 108, 111, 99, + 6, 108, 100, 108, 111, 99, 97, + 5, 115, 116, 108, 111, 99, + 8, 108, 111, 99, 97, 108, 108, 111, 99, + 9, 101, 110, 100, 102, 105, 108, 116, 101, 114, + 10, 117, 110, 97, 108, 105, 103, 110, 101, 100, 46, + 9, 118, 111, 108, 97, 116, 105, 108, 101, 46, + 5, 116, 97, 105, 108, 46, + 7, 105, 110, 105, 116, 111, 98, 106, + 12, 99, 111, 110, 115, 116, 114, 97, 105, 110, 101, 100, 46, + 5, 99, 112, 98, 108, 107, + 7, 105, 110, 105, 116, 98, 108, 107, + 3, 110, 111, 46, + 7, 114, 101, 116, 104, 114, 111, 119, + 6, 115, 105, 122, 101, 111, 102, + 10, 114, 101, 102, 97, 110, 121, 116, 121, 112, 101, + 9, 114, 101, 97, 100, 111, 110, 108, 121, 46, + }; + + names = new string [219]; + + for (int i = 0, p = 0; i < names.Length; i++) { + var buffer = new char [table [p++]]; + + for (int j = 0; j < buffer.Length; j++) + buffer [j] = (char) table [p++]; + + names [i] = new string (buffer); + } + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCodes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCodes.cs index ce468a1..85712ec 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCodes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/OpCodes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/SequencePoint.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/SequencePoint.cs index 189b5f5..ef87e30 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/SequencePoint.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/SequencePoint.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Symbols.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Symbols.cs index ba2e694..426c4a7 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Symbols.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/Symbols.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -151,10 +151,15 @@ namespace Mono.Cecil.Cil { get { return local_var_token; } } - public MethodSymbols (string methodName) + internal MethodSymbols (string methodName) { this.method_name = methodName; } + + public MethodSymbols (MetadataToken methodToken) + { + this.method_token = methodToken; + } } public delegate Instruction InstructionMapper (int offset); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableDefinition.cs index 690543e..f501bca 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableReference.cs index 5453845..ce0b4cd 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/VariableReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/BlobHeap.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/BlobHeap.cs index 81c775e..e7e2928 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/BlobHeap.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/BlobHeap.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs index e8759d7..b63412d 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -261,7 +261,7 @@ namespace Mono.Cecil.Metadata { abstract class HeapBuffer : ByteBuffer { public bool IsLarge { - get { return base.length > 65536; } + get { return base.length > 65535; } } public abstract bool IsEmpty { get; } @@ -274,7 +274,7 @@ namespace Mono.Cecil.Metadata { class StringHeapBuffer : HeapBuffer { - readonly Dictionary<string, uint> strings = new Dictionary<string, uint> (); + readonly Dictionary<string, uint> strings = new Dictionary<string, uint> (StringComparer.Ordinal); public sealed override bool IsEmpty { get { return length <= 1; } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/CodedIndex.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/CodedIndex.cs index 6267726..3e30fd8 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/CodedIndex.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/CodedIndex.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/ElementType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/ElementType.cs index 1ede042..72fc1cc 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/ElementType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/ElementType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/GuidHeap.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/GuidHeap.cs index 19acdab..1adc079 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/GuidHeap.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/GuidHeap.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Heap.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Heap.cs index e7ff720..bc21acd 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Heap.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Heap.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/MetadataToken.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/MetadataToken.cs index 8c1338e..bda56b0 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/MetadataToken.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/MetadataToken.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Row.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Row.cs index ab491a6..dfda00a 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Row.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Row.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/StringHeap.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/StringHeap.cs index 85a2892..4a0a0af 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/StringHeap.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/StringHeap.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TableHeap.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TableHeap.cs index cc441db..9bc0edd 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TableHeap.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TableHeap.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -62,6 +62,8 @@ namespace Mono.Cecil.Metadata { TypeSpec = 0x1b, ImplMap = 0x1c, FieldRVA = 0x1d, + EncLog = 0x1e, + EncMap = 0x1f, Assembly = 0x20, AssemblyProcessor = 0x21, AssemblyOS = 0x22, @@ -88,53 +90,9 @@ namespace Mono.Cecil.Metadata { public long Valid; public long Sorted; - public static readonly Table [] TableIdentifiers = new [] { - Table.Module, - Table.TypeRef, - Table.TypeDef, - Table.FieldPtr, - Table.Field, - Table.MethodPtr, - Table.Method, - Table.ParamPtr, - Table.Param, - Table.InterfaceImpl, - Table.MemberRef, - Table.Constant, - Table.CustomAttribute, - Table.FieldMarshal, - Table.DeclSecurity, - Table.ClassLayout, - Table.FieldLayout, - Table.StandAloneSig, - Table.EventMap, - Table.EventPtr, - Table.Event, - Table.PropertyMap, - Table.PropertyPtr, - Table.Property, - Table.MethodSemantics, - Table.MethodImpl, - Table.ModuleRef, - Table.TypeSpec, - Table.ImplMap, - Table.FieldRVA, - Table.Assembly, - Table.AssemblyProcessor, - Table.AssemblyOS, - Table.AssemblyRef, - Table.AssemblyRefProcessor, - Table.AssemblyRefOS, - Table.File, - Table.ExportedType, - Table.ManifestResource, - Table.NestedClass, - Table.GenericParam, - Table.MethodSpec, - Table.GenericParamConstraint, - }; + public const int TableCount = 45; - public readonly TableInformation [] Tables = new TableInformation [45]; + public readonly TableInformation [] Tables = new TableInformation [TableCount]; public TableInformation this [Table table] { get { return Tables [(int) table]; } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TokenType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TokenType.cs index f822b6d..2c2010f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TokenType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/TokenType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/UserStringHeap.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/UserStringHeap.cs index 7353cb7..5910508 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/UserStringHeap.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/UserStringHeap.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Utilities.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Utilities.cs index 496c61b..d752b24 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Utilities.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/Utilities.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamReader.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamReader.cs index 8f5b38e..ec2477c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamReader.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamReader.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamWriter.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamWriter.cs index 37189cb..2bf6ccc 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamWriter.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/BinaryStreamWriter.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBuffer.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBuffer.cs index 4303b6d..c7ae7ae 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBuffer.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBuffer.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -140,11 +140,16 @@ namespace Mono.Cecil.PE { public int ReadCompressedInt32 () { - var value = (int) ReadCompressedUInt32 (); - - return (value & 1) != 0 - ? -(value >> 1) - : value >> 1; + var value = (int) (ReadCompressedUInt32 () >> 1); + if ((value & 1) == 0) + return value; + if (value < 0x40) + return value - 0x40; + if (value < 0x2000) + return value - 0x2000; + if (value < 0x10000000) + return value - 0x10000000; + return value - 0x20000000; } public float ReadSingle () @@ -267,7 +272,19 @@ namespace Mono.Cecil.PE { public void WriteCompressedInt32 (int value) { - WriteCompressedUInt32 ((uint) ((value < 0) ? ((-value) << 1) | 1 : value << 1)); + if (value >= 0) { + WriteCompressedUInt32 ((uint) (value << 1)); + return; + } + + if (value > -0x40) + value = 0x40 + value; + else if (value >= -0x2000) + value = 0x2000 + value; + else if (value >= -0x20000000) + value = 0x20000000 + value; + + WriteCompressedUInt32 ((uint) ((value << 1) | 1)); } public void WriteBytes (byte [] bytes) diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBufferEqualityComparer.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBufferEqualityComparer.cs index fe56a6f..70e8916 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBufferEqualityComparer.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBufferEqualityComparer.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/DataDirectory.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/DataDirectory.cs index 5ba2d2d..fedd0e2 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/DataDirectory.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/DataDirectory.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Image.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Image.cs index 11aba8f..a11cf1c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Image.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Image.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -41,6 +41,7 @@ namespace Mono.Cecil.PE { public ModuleKind Kind; public TargetRuntime Runtime; public TargetArchitecture Architecture; + public ModuleCharacteristics Characteristics; public string FileName; public Section [] Sections; @@ -52,6 +53,7 @@ namespace Mono.Cecil.PE { public DataDirectory Debug; public DataDirectory Resources; + public DataDirectory StrongName; public StringHeap StringHeap; public BlobHeap BlobHeap; diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs index a228c1f..c96c1db 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -99,13 +99,14 @@ namespace Mono.Cecil.PE { // Characteristics 2 ushort characteristics = ReadUInt16 (); - ushort subsystem; - ReadOptionalHeaders (out subsystem); + ushort subsystem, dll_characteristics; + ReadOptionalHeaders (out subsystem, out dll_characteristics); ReadSections (sections); ReadCLIHeader (); ReadMetadata (); image.Kind = GetModuleKind (characteristics, subsystem); + image.Characteristics = (ModuleCharacteristics) dll_characteristics; } TargetArchitecture ReadArchitecture () @@ -118,6 +119,8 @@ namespace Mono.Cecil.PE { return TargetArchitecture.AMD64; case 0x0200: return TargetArchitecture.IA64; + case 0x01c4: + return TargetArchitecture.ARMv7; } throw new NotSupportedException (); @@ -134,7 +137,7 @@ namespace Mono.Cecil.PE { return ModuleKind.Console; } - void ReadOptionalHeaders (out ushort subsystem) + void ReadOptionalHeaders (out ushort subsystem, out ushort dll_characteristics) { // - PEOptionalHeader // - StandardFieldsHeader @@ -174,6 +177,7 @@ namespace Mono.Cecil.PE { subsystem = ReadUInt16 (); // DLLFlags 2 + dll_characteristics = ReadUInt16 (); // StackReserveSize 4 || 8 // StackCommitSize 4 || 8 // HeapReserveSize 4 || 8 @@ -190,7 +194,7 @@ namespace Mono.Cecil.PE { // CertificateTable 8 // BaseRelocationTable 8 - Advance (pe64 ? 90 : 74); + Advance (pe64 ? 88 : 72); // Debug 8 image.Debug = ReadDataDirectory (); @@ -276,9 +280,6 @@ namespace Mono.Cecil.PE { sections [i] = section; - if (section.Name == ".reloc") - continue; - ReadSectionData (section); } @@ -323,6 +324,7 @@ namespace Mono.Cecil.PE { // Resources 8 image.Resources = ReadDataDirectory (); // StrongNameSignature 8 + image.StrongName = ReadDataDirectory (); // CodeManagerTable 8 // VTableFixups 8 // ExportAddressTableJumps 8 @@ -416,12 +418,11 @@ namespace Mono.Cecil.PE { // Sorted 8 heap.Sorted = ReadInt64 (); - for (int i = 0; i < TableHeap.TableIdentifiers.Length; i++) { - var table = TableHeap.TableIdentifiers [i]; - if (!heap.HasTable (table)) + for (int i = 0; i < TableHeap.TableCount; i++) { + if (!heap.HasTable ((Table) i)) continue; - heap.Tables [(int) table].Length = ReadUInt32 (); + heap.Tables [i].Length = ReadUInt32 (); } SetIndexSize (image.StringHeap, sizes, 0x1); @@ -459,8 +460,8 @@ namespace Mono.Cecil.PE { var heap = image.TableHeap; var tables = heap.Tables; - for (int i = 0; i < TableHeap.TableIdentifiers.Length; i++) { - var table = TableHeap.TableIdentifiers [i]; + for (int i = 0; i < TableHeap.TableCount; i++) { + var table = (Table) i; if (!heap.HasTable (table)) continue; @@ -595,6 +596,10 @@ namespace Mono.Cecil.PE { size = 4 // RVA + GetTableIndexSize (Table.Field); // Field break; + case Table.EncLog: + case Table.EncMap: + size = 4; + break; case Table.Assembly: size = 16 // HashAlgId 4, Version 4 * 2, Flags 4 + blobidx_size // PublicKey @@ -655,12 +660,10 @@ namespace Mono.Cecil.PE { throw new NotSupportedException (); } - int index = (int) table; - - tables [index].RowSize = (uint) size; - tables [index].Offset = offset; + tables [i].RowSize = (uint) size; + tables [i].Offset = offset; - offset += (uint) size * tables [index].Length; + offset += (uint) size * tables [i].Length; } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs index 939de6b..08f9ea2 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -58,6 +58,7 @@ namespace Mono.Cecil.PE { internal const RVA text_rva = 0x2000; readonly bool pe64; + readonly bool has_reloc; readonly uint time_stamp; internal Section text; @@ -71,11 +72,12 @@ namespace Mono.Cecil.PE { { this.module = module; this.metadata = metadata; + this.pe64 = module.Architecture == TargetArchitecture.AMD64 || module.Architecture == TargetArchitecture.IA64; + this.has_reloc = module.Architecture == TargetArchitecture.I386; this.GetDebugHeader (); this.GetWin32Resources (); this.text_map = BuildTextMap (); - this.sections = 2; // text + reloc - this.pe64 = module.Architecture != TargetArchitecture.I386; + this.sections = (ushort) (has_reloc ? 2 : 1); // text + reloc? this.time_stamp = (uint) DateTime.UtcNow.Subtract (new DateTime (1970, 1, 1)).TotalSeconds; } @@ -133,7 +135,8 @@ namespace Mono.Cecil.PE { previous = rsrc; } - reloc = CreateSection (".reloc", 12u, previous); + if (has_reloc) + reloc = CreateSection (".reloc", 12u, previous); } Section CreateSection (string name, uint size, Section previous) @@ -212,25 +215,36 @@ namespace Mono.Cecil.PE { return 0x8664; case TargetArchitecture.IA64: return 0x0200; + case TargetArchitecture.ARMv7: + return 0x01c4; } throw new NotSupportedException (); } + Section LastSection () + { + if (reloc != null) + return reloc; + + if (rsrc != null) + return rsrc; + + return text; + } + void WriteOptionalHeaders () { WriteUInt16 ((ushort) (!pe64 ? 0x10b : 0x20b)); // Magic WriteByte (8); // LMajor WriteByte (0); // LMinor WriteUInt32 (text.SizeOfRawData); // CodeSize - WriteUInt32 (reloc.SizeOfRawData + WriteUInt32 ((reloc != null ? reloc.SizeOfRawData : 0) + (rsrc != null ? rsrc.SizeOfRawData : 0)); // InitializedDataSize WriteUInt32 (0); // UninitializedDataSize - var entry_point_rva = text_map.GetRVA (TextSegment.StartupStub); - if (module.Architecture == TargetArchitecture.IA64) - entry_point_rva += 0x20; - WriteUInt32 (entry_point_rva); // EntryPointRVA + var startub_stub = text_map.GetRange (TextSegment.StartupStub); + WriteUInt32 (startub_stub.Length > 0 ? startub_stub.Start : 0); // EntryPointRVA WriteUInt32 (text_rva); // BaseOfCode if (!pe64) { @@ -251,12 +265,13 @@ namespace Mono.Cecil.PE { WriteUInt16 (0); // SubSysMinor WriteUInt32 (0); // Reserved - WriteUInt32 (reloc.VirtualAddress + Align (reloc.VirtualSize, section_alignment)); // ImageSize + var last_section = LastSection(); + WriteUInt32 (last_section.VirtualAddress + Align (last_section.VirtualSize, section_alignment)); // ImageSize WriteUInt32 (text.PointerToRawData); // HeaderSize WriteUInt32 (0); // Checksum WriteUInt16 (GetSubSystem ()); // SubSystem - WriteUInt16 (0x8540); // DLLFlags + WriteUInt16 ((ushort) module.Characteristics); // DLLFlags const ulong stack_reserve = 0x100000; const ulong stack_commit = 0x1000; @@ -288,8 +303,8 @@ namespace Mono.Cecil.PE { WriteZeroDataDirectory (); // ExceptionTable WriteZeroDataDirectory (); // CertificateTable - WriteUInt32 (reloc.VirtualAddress); // BaseRelocationTable - WriteUInt32 (reloc.VirtualSize); + WriteUInt32 (reloc != null ? reloc.VirtualAddress : 0); // BaseRelocationTable + WriteUInt32 (reloc != null ? reloc.VirtualSize : 0); if (text_map.GetLength (TextSegment.DebugDirectory) > 0) { WriteUInt32 (text_map.GetRVA (TextSegment.DebugDirectory)); @@ -335,7 +350,8 @@ namespace Mono.Cecil.PE { if (rsrc != null) WriteSection (rsrc, 0x40000040); - WriteSection (reloc, 0x42000040); + if (reloc != null) + WriteSection (reloc, 0x42000040); } void WriteSection (Section section, uint characteristics) @@ -386,8 +402,10 @@ namespace Mono.Cecil.PE { // ImportAddressTable - WriteRVA (text_map.GetRVA (TextSegment.ImportHintNameTable)); - WriteRVA (0); + if (has_reloc) { + WriteRVA (text_map.GetRVA (TextSegment.ImportHintNameTable)); + WriteRVA (0); + } // CLIHeader @@ -439,6 +457,9 @@ namespace Mono.Cecil.PE { WriteDebugDirectory (); } + if (!has_reloc) + return; + // ImportDirectory MoveToRVA (TextSegment.ImportDirectory); WriteImportDirectory (); @@ -605,19 +626,8 @@ namespace Mono.Cecil.PE { WriteUInt16 (0x25ff); WriteUInt32 ((uint) image_base + text_map.GetRVA (TextSegment.ImportAddressTable)); return; - case TargetArchitecture.AMD64: - WriteUInt16 (0xa148); - WriteUInt32 ((uint) image_base + text_map.GetRVA (TextSegment.ImportAddressTable)); - WriteUInt16 (0xe0ff); - return; - case TargetArchitecture.IA64: - WriteBytes (new byte [] { - 0x0b, 0x48, 0x00, 0x02, 0x18, 0x10, 0xa0, 0x40, 0x24, 0x30, 0x28, 0x00, 0x00, 0x00, 0x04, 0x00, - 0x10, 0x08, 0x00, 0x12, 0x18, 0x10, 0x60, 0x50, 0x04, 0x80, 0x03, 0x00, 0x60, 0x00, 0x80, 0x00 - }); - WriteUInt32 ((uint) image_base + text_map.GetRVA (TextSegment.StartupStub)); - WriteUInt32 ((uint) image_base + text_rva); - return; + default: + throw new NotSupportedException (); } } @@ -642,13 +652,8 @@ namespace Mono.Cecil.PE { case TargetArchitecture.I386: WriteUInt32 (0x3000 + reloc_rva - page_rva); break; - case TargetArchitecture.AMD64: - WriteUInt32 (0xa000 + reloc_rva - page_rva); - break; - case TargetArchitecture.IA64: - WriteUInt16 ((ushort) (0xa000 + reloc_rva - page_rva)); - WriteUInt16 ((ushort) (0xa000 + reloc_rva - page_rva + 8)); - break; + default: + throw new NotSupportedException(); } WriteBytes (new byte [file_alignment - reloc.VirtualSize]); @@ -663,7 +668,8 @@ namespace Mono.Cecil.PE { WriteText (); if (rsrc != null) WriteRsrc (); - WriteReloc (); + if (reloc != null) + WriteReloc (); } TextMap BuildTextMap () @@ -694,8 +700,16 @@ namespace Mono.Cecil.PE { map.AddMap (TextSegment.DebugDirectory, debug_dir_len, 4); + if (!has_reloc) { + var start = map.GetNextRVA (TextSegment.DebugDirectory); + map.AddMap (TextSegment.ImportDirectory, new Range (start, 0)); + map.AddMap (TextSegment.ImportHintNameTable, new Range (start, 0)); + map.AddMap (TextSegment.StartupStub, new Range (start, 0)); + return map; + } + RVA import_dir_rva = map.GetNextRVA (TextSegment.DebugDirectory); - RVA import_hnt_rva = import_dir_rva + (!pe64 ? 48u : 52u); + RVA import_hnt_rva = import_dir_rva + 48u; import_hnt_rva = (import_hnt_rva + 15u) & ~15u; uint import_dir_len = (import_hnt_rva - import_dir_rva) + 27u; @@ -716,12 +730,8 @@ namespace Mono.Cecil.PE { switch (module.Architecture) { case TargetArchitecture.I386: return 6; - case TargetArchitecture.AMD64: - return 12; - case TargetArchitecture.IA64: - return 48; default: - throw new InvalidOperationException (); + throw new NotSupportedException (); } } @@ -744,24 +754,22 @@ namespace Mono.Cecil.PE { int GetStrongNameLength () { - if ((module.Attributes & ModuleAttributes.StrongNameSigned) == 0) - return 0; - if (module.Assembly == null) - throw new InvalidOperationException (); + return 0; var public_key = module.Assembly.Name.PublicKey; + if (public_key.IsNullOrEmpty ()) + return 0; - if (public_key != null) { - // in fx 2.0 the key may be from 384 to 16384 bits - // so we must calculate the signature size based on - // the size of the public key (minus the 32 byte header) - int size = public_key.Length; - if (size > 32) - return size - 32; - // note: size == 16 for the ECMA "key" which is replaced - // by the runtime with a 1024 bits key (128 bytes) - } + // in fx 2.0 the key may be from 384 to 16384 bits + // so we must calculate the signature size based on + // the size of the public key (minus the 32 byte header) + int size = public_key.Length; + if (size > 32) + return size - 32; + + // note: size == 16 for the ECMA "key" which is replaced + // by the runtime with a 1024 bits key (128 bytes) return 128; // default strongname signature size } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Section.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Section.cs index 54935f9..6a4c7ba 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Section.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/Section.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/TextMap.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/TextMap.cs index daeda0c..047a4c1 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/TextMap.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/TextMap.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ArrayType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ArrayType.cs index e3a060f..55c59d8 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ArrayType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ArrayType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyDefinition.cs index bb757d3..f1b5d25 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -64,7 +64,7 @@ namespace Mono.Cecil { if (main_module.HasImage) return modules = main_module.Read (this, (_, reader) => reader.ReadModules ()); - return modules = new Collection<ModuleDefinition> { main_module }; + return modules = new Collection<ModuleDefinition> (1) { main_module }; } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyFlags.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyFlags.cs index 6d04c59..72a0eb0 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyFlags.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyFlags.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -35,6 +35,7 @@ namespace Mono.Cecil { PublicKey = 0x0001, SideBySideCompatible = 0x0000, Retargetable = 0x0100, + WindowsRuntime = 0x0200, DisableJITCompileOptimizer = 0x4000, EnableJITCompileTracking = 0x8000, } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyHashAlgorithm.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyHashAlgorithm.cs index 0e7bf83..79a5699 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyHashAlgorithm.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyHashAlgorithm.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs index b701981..8a9f809 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -32,15 +32,15 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle ("Mono.Cecil")] [assembly: AssemblyProduct ("Mono.Cecil")] -[assembly: AssemblyCopyright ("Copyright © 2008 - 2010 Jb Evain")] +[assembly: AssemblyCopyright ("Copyright © 2008 - 2011 Jb Evain")] [assembly: ComVisible (false)] [assembly: Guid ("fd225bb4-fa53-44b2-a6db-85f5e48dcb54")] -[assembly: AssemblyVersion ("0.9.4.0")] +[assembly: AssemblyVersion ("0.9.5.0")] #if !CF -[assembly: AssemblyFileVersion ("0.9.4.0")] +[assembly: AssemblyFileVersion ("0.9.5.0")] #endif [assembly: InternalsVisibleTo ("Mono.Cecil.Pdb, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")] diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyLinkedResource.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyLinkedResource.cs index 68e44f8..4d8bac0 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyLinkedResource.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyLinkedResource.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameDefinition.cs index 4756cb8..dc6b905 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs index 2798082..4f58fe2 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -92,8 +92,13 @@ namespace Mono.Cecil { set { attributes = attributes.SetAttributes ((uint) AssemblyAttributes.Retargetable, value); } } + public bool IsWindowsRuntime { + get { return attributes.GetAttributes ((uint) AssemblyAttributes.WindowsRuntime); } + set { attributes = attributes.SetAttributes ((uint) AssemblyAttributes.WindowsRuntime, value); } + } + public byte [] PublicKey { - get { return public_key; } + get { return public_key ?? Empty<byte>.Array; } set { public_key = value; HasPublicKey = !public_key.IsNullOrEmpty (); @@ -111,7 +116,7 @@ namespace Mono.Cecil { Array.Copy (hash, (hash.Length - 8), public_key_token, 0, 8); Array.Reverse (public_key_token, 0, 8); } - return public_key_token; + return public_key_token ?? Empty<byte>.Array; } set { public_key_token = value; @@ -170,9 +175,10 @@ namespace Mono.Cecil { builder.Append (sep); builder.Append ("PublicKeyToken="); - if (this.PublicKeyToken != null && public_key_token.Length > 0) { - for (int i = 0 ; i < public_key_token.Length ; i++) { - builder.Append (public_key_token [i].ToString ("x2")); + var pk_token = PublicKeyToken; + if (!pk_token.IsNullOrEmpty () && pk_token.Length > 0) { + for (int i = 0 ; i < pk_token.Length ; i++) { + builder.Append (pk_token [i].ToString ("x2")); } } else builder.Append ("null"); @@ -202,22 +208,22 @@ namespace Mono.Cecil { if (parts.Length != 2) throw new ArgumentException ("Malformed name"); - switch (parts [0]) { - case "Version": + switch (parts [0].ToLowerInvariant ()) { + case "version": name.Version = new Version (parts [1]); break; - case "Culture": + case "culture": name.Culture = parts [1]; break; - case "PublicKeyToken": - string pk_token = parts [1]; + case "publickeytoken": + var pk_token = parts [1]; if (pk_token == "null") break; name.PublicKeyToken = new byte [pk_token.Length / 2]; - for (int j = 0; j < name.PublicKeyToken.Length; j++) { + for (int j = 0; j < name.PublicKeyToken.Length; j++) name.PublicKeyToken [j] = Byte.Parse (pk_token.Substring (j * 2, 2), NumberStyles.HexNumber); - } + break; } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs index 2cafb99..a9f0b6a 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -85,6 +85,9 @@ namespace Mono.Cecil { if (parameters.AssemblyResolver != null) module.assembly_resolver = parameters.AssemblyResolver; + if (parameters.MetadataResolver != null) + module.metadata_resolver = parameters.MetadataResolver; + return module; } @@ -374,7 +377,7 @@ namespace Mono.Cecil { this.image = module.Image; this.module = module; this.metadata = module.MetadataSystem; - this.code = CodeReader.CreateCodeReader (this); + this.code = new CodeReader (image.MetadataSection, this); } int GetCodedIndexSize (CodedIndex index) @@ -487,36 +490,48 @@ namespace Mono.Cecil { return module; } - public Collection<AssemblyNameReference> ReadAssemblyReferences () + void InitializeAssemblyReferences () { + if (metadata.AssemblyReferences != null) + return; + int length = MoveTo (Table.AssemblyRef); - var references = new Collection<AssemblyNameReference> (length); + var references = metadata.AssemblyReferences = new AssemblyNameReference [length]; - for (uint i = 1; i <= length; i++) { + for (uint i = 0; i < length; i++) { var reference = new AssemblyNameReference (); - reference.token = new MetadataToken (TokenType.AssemblyRef, i); + reference.token = new MetadataToken (TokenType.AssemblyRef, i + 1); PopulateVersionAndFlags (reference); - reference.PublicKeyToken = ReadBlob (); + var key_or_token = ReadBlob (); + + if (reference.HasPublicKey) + reference.PublicKey = key_or_token; + else + reference.PublicKeyToken = key_or_token; PopulateNameAndCulture (reference); reference.Hash = ReadBlob (); - references.Add (reference); + references [i] = reference; } + } + + public Collection<AssemblyNameReference> ReadAssemblyReferences () + { + InitializeAssemblyReferences (); - return references; + return new Collection<AssemblyNameReference> (metadata.AssemblyReferences); } public MethodDefinition ReadEntryPoint () { - if (module.Kind != ModuleKind.Console && module.Kind != ModuleKind.Windows) + if (module.Image.EntryPointToken == 0) return null; var token = new MetadataToken (module.Image.EntryPointToken); - return GetMethodDefinition (token.RID); } @@ -537,6 +552,7 @@ namespace Mono.Cecil { var parameters = new ReaderParameters { ReadingMode = module.ReadingMode, SymbolReaderProvider = module.SymbolReaderProvider, + AssemblyResolver = module.AssemblyResolver }; modules.Add (ModuleDefinition.ReadModule ( @@ -555,19 +571,27 @@ namespace Mono.Cecil { return Path.Combine (path, name); } - public Collection<ModuleReference> ReadModuleReferences () + void InitializeModuleReferences () { + if (metadata.ModuleReferences != null) + return; + int length = MoveTo (Table.ModuleRef); - var references = new Collection<ModuleReference> (length); + var references = metadata.ModuleReferences = new ModuleReference [length]; - for (uint i = 1; i <= length; i++) { + for (uint i = 0; i < length; i++) { var reference = new ModuleReference (ReadString ()); - reference.token = new MetadataToken (TokenType.ModuleRef, i); + reference.token = new MetadataToken (TokenType.ModuleRef, i + 1); - references.Add (reference); + references [i] = reference; } + } + + public Collection<ModuleReference> ReadModuleReferences () + { + InitializeModuleReferences (); - return references; + return new Collection<ModuleReference> (metadata.ModuleReferences); } public bool HasFileResource () @@ -682,9 +706,24 @@ namespace Mono.Cecil { types.Add (type); } + if (image.HasTable (Table.MethodPtr) || image.HasTable (Table.FieldPtr)) + CompleteTypes (); + return types; } + void CompleteTypes () + { + var types = metadata.Types; + + for (int i = 0; i < types.Length; i++) { + var type = types [i]; + + InitializeCollection (type.Fields); + InitializeCollection (type.Methods); + } + } + void InitializeTypeDefinitions () { if (metadata.Types != null) @@ -740,8 +779,12 @@ namespace Mono.Cecil { var nested_types = new MemberDefinitionCollection<TypeDefinition> (type, mapping.Length); - for (int i = 0; i < mapping.Length; i++) - nested_types.Add (GetTypeDefinition (mapping [i])); + for (int i = 0; i < mapping.Length; i++) { + var nested_type = GetTypeDefinition (mapping [i]); + + if (nested_type != null) + nested_types.Add (nested_type); + } metadata.RemoveNestedTypeMapping (type); @@ -995,7 +1038,7 @@ namespace Mono.Cecil { type.scope = scope; type.DeclaringType = declaring_type; - MetadataSystem.TryProcessPrimitiveType (type); + MetadataSystem.TryProcessPrimitiveTypeReference (type); return type; } @@ -1003,13 +1046,12 @@ namespace Mono.Cecil { IMetadataScope GetTypeReferenceScope (MetadataToken scope) { switch (scope.TokenType) { - // FIXME: both assembly refs and module refs should be in their - // own arrays, in case of someone modify the collections before - // this code is called case TokenType.AssemblyRef: - return module.AssemblyReferences [(int) scope.RID - 1]; + InitializeAssemblyReferences (); + return metadata.AssemblyReferences [(int) scope.RID - 1]; case TokenType.ModuleRef: - return module.ModuleReferences [(int) scope.RID - 1]; + InitializeModuleReferences (); + return metadata.ModuleReferences [(int) scope.RID - 1]; case TokenType.Module: return module; default: @@ -1108,14 +1150,19 @@ namespace Mono.Cecil { var fields = new MemberDefinitionCollection<FieldDefinition> (type, (int) fields_range.Length); this.context = type; - MoveTo (Table.Field, fields_range.Start); - for (uint i = 0; i < fields_range.Length; i++) - fields.Add (ReadField (fields_range.Start + i)); + if (!MoveTo (Table.FieldPtr, fields_range.Start)) { + if (!MoveTo (Table.Field, fields_range.Start)) + return fields; + + for (uint i = 0; i < fields_range.Length; i++) + ReadField (fields_range.Start + i, fields); + } else + ReadPointers (Table.FieldPtr, Table.Field, fields_range, fields, ReadField); return fields; } - FieldDefinition ReadField (uint field_rid) + void ReadField (uint field_rid, Collection<FieldDefinition> fields) { var attributes = (FieldAttributes) ReadUInt16 (); var name = ReadString (); @@ -1125,7 +1172,10 @@ namespace Mono.Cecil { field.token = new MetadataToken (TokenType.Field, field_rid); metadata.AddFieldDefinition (field); - return field; + if (IsDeleted (field)) + return; + + fields.Add (field); } void InitializeFields () @@ -1293,18 +1343,24 @@ namespace Mono.Cecil { metadata.RemoveEventsRange (type); - if (range.Length == 0 || !MoveTo (Table.Event, range.Start)) + if (range.Length == 0) return events; this.context = type; - for (uint i = 0; i < range.Length; i++) - events.Add (ReadEvent (range.Start + i)); + if (!MoveTo (Table.EventPtr, range.Start)) { + if (!MoveTo (Table.Event, range.Start)) + return events; + + for (uint i = 0; i < range.Length; i++) + ReadEvent (range.Start + i, events); + } else + ReadPointers (Table.EventPtr, Table.Event, range, events, ReadEvent); return events; } - EventDefinition ReadEvent (uint event_rid) + void ReadEvent (uint event_rid, Collection<EventDefinition> events) { var attributes = (EventAttributes) ReadUInt16 (); var name = ReadString (); @@ -1312,7 +1368,11 @@ namespace Mono.Cecil { var @event = new EventDefinition (name, attributes, event_type); @event.token = new MetadataToken (TokenType.Event, event_rid); - return @event; + + if (IsDeleted (@event)) + return; + + events.Add (@event); } void InitializeEvents () @@ -1360,18 +1420,23 @@ namespace Mono.Cecil { var properties = new MemberDefinitionCollection<PropertyDefinition> (type, (int) range.Length); - if (range.Length == 0 || !MoveTo (Table.Property, range.Start)) + if (range.Length == 0) return properties; this.context = type; - for (uint i = 0; i < range.Length; i++) - properties.Add (ReadProperty (range.Start + i)); + if (!MoveTo (Table.PropertyPtr, range.Start)) { + if (!MoveTo (Table.Property, range.Start)) + return properties; + for (uint i = 0; i < range.Length; i++) + ReadProperty (range.Start + i, properties); + } else + ReadPointers (Table.PropertyPtr, Table.Property, range, properties, ReadProperty); return properties; } - PropertyDefinition ReadProperty (uint property_rid) + void ReadProperty (uint property_rid, Collection<PropertyDefinition> properties) { var attributes = (PropertyAttributes) ReadUInt16 (); var name = ReadString (); @@ -1393,7 +1458,10 @@ namespace Mono.Cecil { property.HasThis = has_this; property.token = new MetadataToken (TokenType.Property, property_rid); - return property; + if (IsDeleted (property)) + return; + + properties.Add (property); } void InitializeProperties () @@ -1562,14 +1630,36 @@ namespace Mono.Cecil { return new MemberDefinitionCollection<MethodDefinition> (type); var methods = new MemberDefinitionCollection<MethodDefinition> (type, (int) methods_range.Length); + if (!MoveTo (Table.MethodPtr, methods_range.Start)) { + if (!MoveTo (Table.Method, methods_range.Start)) + return methods; - MoveTo (Table.Method, methods_range.Start); - for (uint i = 0; i < methods_range.Length; i++) - ReadMethod (methods_range.Start + i, methods); + for (uint i = 0; i < methods_range.Length; i++) + ReadMethod (methods_range.Start + i, methods); + } else + ReadPointers (Table.MethodPtr, Table.Method, methods_range, methods, ReadMethod); return methods; } + void ReadPointers<TMember> (Table ptr, Table table, Range range, Collection<TMember> members, Action<uint, Collection<TMember>> reader) + where TMember : IMemberDefinition + { + for (uint i = 0; i < range.Length; i++) { + MoveTo (ptr, range.Start + i); + + var rid = ReadTableIndex (table); + MoveTo (table, rid); + + reader (rid, members); + } + } + + static bool IsDeleted (IMemberDefinition member) + { + return member.IsSpecialName && member.Name == "_Deleted"; + } + void InitializeMethods () { if (metadata.Methods != null) @@ -1587,6 +1677,9 @@ namespace Mono.Cecil { method.Name = ReadString (); method.token = new MetadataToken (TokenType.Method, method_rid); + if (IsDeleted (method)) + return; + methods.Add (method); // attach method var signature = ReadBlobIndex (); @@ -1607,22 +1700,44 @@ namespace Mono.Cecil { void ReadParameters (MethodDefinition method, Range param_range) { - MoveTo (Table.Param, param_range.Start); - for (uint i = 0; i < param_range.Length; i++) { - var attributes = (ParameterAttributes) ReadUInt16 (); - var sequence = ReadUInt16 (); - var name = ReadString (); + if (!MoveTo (Table.ParamPtr, param_range.Start)) { + if (!MoveTo (Table.Param, param_range.Start)) + return; - var parameter = sequence == 0 - ? method.MethodReturnType.Parameter - : method.Parameters [sequence - 1]; + for (uint i = 0; i < param_range.Length; i++) + ReadParameter (param_range.Start + i, method); + } else + ReadParameterPointers (method, param_range); + } + + void ReadParameterPointers (MethodDefinition method, Range range) + { + for (uint i = 0; i < range.Length; i++) { + MoveTo (Table.ParamPtr, range.Start + i); + + var rid = ReadTableIndex (Table.Param); - parameter.token = new MetadataToken (TokenType.Param, param_range.Start + i); - parameter.Name = name; - parameter.Attributes = attributes; + MoveTo (Table.Param, rid); + + ReadParameter (rid, method); } } + void ReadParameter (uint param_rid, MethodDefinition method) + { + var attributes = (ParameterAttributes) ReadUInt16 (); + var sequence = ReadUInt16 (); + var name = ReadString (); + + var parameter = sequence == 0 + ? method.MethodReturnType.Parameter + : method.Parameters [sequence - 1]; + + parameter.token = new MetadataToken (TokenType.Param, param_rid); + parameter.Name = name; + parameter.Attributes = attributes; + } + void ReadMethodSignature (uint signature, IMethodSignature method) { var reader = ReadSignature (signature); @@ -1673,25 +1788,35 @@ namespace Mono.Cecil { { InitializeGenericParameters (); - Range range; - if (!metadata.TryGetGenericParameterRange (provider, out range)) + Range [] ranges; + if (!metadata.TryGetGenericParameterRanges (provider, out ranges)) return false; - return range.Length > 0; + return RangesSize (ranges) > 0; } public Collection<GenericParameter> ReadGenericParameters (IGenericParameterProvider provider) { InitializeGenericParameters (); - Range range; - if (!metadata.TryGetGenericParameterRange (provider, out range) - || !MoveTo (Table.GenericParam, range.Start)) - return new Collection<GenericParameter> (); + Range [] ranges; + if (!metadata.TryGetGenericParameterRanges (provider, out ranges)) + return new GenericParameterCollection (provider); metadata.RemoveGenericParameterRange (provider); - var generic_parameters = new Collection<GenericParameter> ((int) range.Length); + var generic_parameters = new GenericParameterCollection (provider, RangesSize (ranges)); + + for (int i = 0; i < ranges.Length; i++) + ReadGenericParametersRange (ranges [i], provider, generic_parameters); + + return generic_parameters; + } + + void ReadGenericParametersRange (Range range, IGenericParameterProvider provider, GenericParameterCollection generic_parameters) + { + if (!MoveTo (Table.GenericParam, range.Start)) + return; for (uint i = 0; i < range.Length; i++) { ReadUInt16 (); // index @@ -1705,8 +1830,6 @@ namespace Mono.Cecil { generic_parameters.Add (parameter); } - - return generic_parameters; } void InitializeGenericParameters () @@ -1723,10 +1846,10 @@ namespace Mono.Cecil { }); } - Dictionary<MetadataToken, Range> InitializeRanges (Table table, Func<MetadataToken> get_next) + Dictionary<MetadataToken, Range []> InitializeRanges (Table table, Func<MetadataToken> get_next) { int length = MoveTo (table); - var ranges = new Dictionary<MetadataToken, Range> (length); + var ranges = new Dictionary<MetadataToken, Range []> (length); if (length == 0) return ranges; @@ -1741,20 +1864,34 @@ namespace Mono.Cecil { owner = next; range.Length++; } else if (next != owner) { - if (owner.RID != 0) - ranges.Add (owner, range); + AddRange (ranges, owner, range); range = new Range (i, 1); owner = next; } else range.Length++; } - if (owner != MetadataToken.Zero) - ranges.Add (owner, range); + AddRange (ranges, owner, range); return ranges; } + static void AddRange (Dictionary<MetadataToken, Range []> ranges, MetadataToken owner, Range range) + { + if (owner.RID == 0) + return; + + Range [] slots; + if (!ranges.TryGetValue (owner, out slots)) { + ranges.Add (owner, new [] { range }); + return; + } + + slots = slots.Resize (slots.Length + 1); + slots [slots.Length - 1] = range; + ranges [owner] = slots; + } + public bool HasGenericConstraints (GenericParameter generic_parameter) { InitializeGenericConstraints (); @@ -1884,6 +2021,8 @@ namespace Mono.Cecil { ReadMethodSignature (signature, call_site); + call_site.MetadataToken = token; + return call_site; } @@ -2075,7 +2214,8 @@ namespace Mono.Cecil { { var declaring_type = GetTypeDefOrRef (type); - this.context = declaring_type; + if (!declaring_type.IsArray) + this.context = declaring_type; var member = ReadMemberReferenceSignature (signature, declaring_type); member.Name = name; @@ -2215,23 +2355,35 @@ namespace Mono.Cecil { { InitializeCustomAttributes (); - Range range; - if (!metadata.TryGetCustomAttributeRange (owner, out range)) + Range [] ranges; + if (!metadata.TryGetCustomAttributeRanges (owner, out ranges)) return false; - return range.Length > 0; + return RangesSize (ranges) > 0; } public Collection<CustomAttribute> ReadCustomAttributes (ICustomAttributeProvider owner) { InitializeCustomAttributes (); - Range range; - if (!metadata.TryGetCustomAttributeRange (owner, out range) - || !MoveTo (Table.CustomAttribute, range.Start)) + Range [] ranges; + if (!metadata.TryGetCustomAttributeRanges (owner, out ranges)) return new Collection<CustomAttribute> (); - var custom_attributes = new Collection<CustomAttribute> ((int) range.Length); + var custom_attributes = new Collection<CustomAttribute> (RangesSize (ranges)); + + for (int i = 0; i < ranges.Length; i++) + ReadCustomAttributeRange (ranges [i], custom_attributes); + + metadata.RemoveCustomAttributeRange (owner); + + return custom_attributes; + } + + void ReadCustomAttributeRange (Range range, Collection<CustomAttribute> custom_attributes) + { + if (!MoveTo (Table.CustomAttribute, range.Start)) + return; for (int i = 0; i < range.Length; i++) { ReadMetadataToken (CodedIndex.HasCustomAttribute); @@ -2243,10 +2395,15 @@ namespace Mono.Cecil { custom_attributes.Add (new CustomAttribute (signature, constructor)); } + } - metadata.RemoveCustomAttributeRange (owner); + static int RangesSize (Range [] ranges) + { + uint size = 0; + for (int i = 0; i < ranges.Length; i++) + size += ranges [i].Length; - return custom_attributes; + return (int) size; } public byte [] ReadCustomAttributeBlob (uint signature) @@ -2257,8 +2414,12 @@ namespace Mono.Cecil { public void ReadCustomAttributeSignature (CustomAttribute attribute) { var reader = ReadSignature (attribute.signature); + + if (!reader.CanReadMore ()) + return; + if (reader.ReadUInt16 () != 0x0001) - throw new InvalidOperationException (); + throw new InvalidOperationException (); var constructor = attribute.Constructor; if (constructor.HasParameters) @@ -2334,23 +2495,35 @@ namespace Mono.Cecil { { InitializeSecurityDeclarations (); - Range range; - if (!metadata.TryGetSecurityDeclarationRange (owner, out range)) + Range [] ranges; + if (!metadata.TryGetSecurityDeclarationRanges (owner, out ranges)) return false; - return range.Length > 0; + return RangesSize (ranges) > 0; } public Collection<SecurityDeclaration> ReadSecurityDeclarations (ISecurityDeclarationProvider owner) { InitializeSecurityDeclarations (); - Range range; - if (!metadata.TryGetSecurityDeclarationRange (owner, out range) - || !MoveTo (Table.DeclSecurity, range.Start)) + Range [] ranges; + if (!metadata.TryGetSecurityDeclarationRanges (owner, out ranges)) return new Collection<SecurityDeclaration> (); - var security_declarations = new Collection<SecurityDeclaration> ((int) range.Length); + var security_declarations = new Collection<SecurityDeclaration> (RangesSize (ranges)); + + for (int i = 0; i < ranges.Length; i++) + ReadSecurityDeclarationRange (ranges [i], security_declarations); + + metadata.RemoveSecurityDeclarationRange (owner); + + return security_declarations; + } + + void ReadSecurityDeclarationRange (Range range, Collection<SecurityDeclaration> security_declarations) + { + if (!MoveTo (Table.DeclSecurity, range.Start)) + return; for (int i = 0; i < range.Length; i++) { var action = (SecurityAction) ReadUInt16 (); @@ -2359,10 +2532,6 @@ namespace Mono.Cecil { security_declarations.Add (new SecurityDeclaration (action, signature, module)); } - - metadata.RemoveSecurityDeclarationRange (owner); - - return security_declarations; } public byte [] ReadSecurityDeclarationBlob (uint signature) @@ -2380,7 +2549,7 @@ namespace Mono.Cecil { return; } - reader.ReadByte (); + reader.position++; var count = reader.ReadCompressedUInt32 (); var attributes = new Collection<SecurityAttribute> ((int) count); @@ -2440,7 +2609,7 @@ namespace Mono.Cecil { break; } - var exported_type = new ExportedType (@namespace, name, scope) { + var exported_type = new ExportedType (@namespace, name, module, scope) { Attributes = attributes, Identifier = (int) identifier, DeclaringType = declaring_type, @@ -2455,21 +2624,24 @@ namespace Mono.Cecil { IMetadataScope GetExportedTypeScope (MetadataToken token) { + var position = this.position; + IMetadataScope scope; + switch (token.TokenType) { case TokenType.AssemblyRef: - return module.AssemblyReferences [(int) token.RID - 1]; + InitializeAssemblyReferences (); + scope = metadata.AssemblyReferences [(int) token.RID - 1]; + break; case TokenType.File: - var position = this.position; - var reference = GetModuleReferenceFromFile (token); - this.position = position; - - if (reference == null) - throw new NotSupportedException (); - - return reference; + InitializeModuleReferences (); + scope = GetModuleReferenceFromFile (token); + break; default: throw new NotSupportedException (); } + + this.position = position; + return scope; } ModuleReference GetModuleReferenceFromFile (MetadataToken token) @@ -2481,16 +2653,15 @@ namespace Mono.Cecil { var file_name = ReadString (); var modules = module.ModuleReferences; - ModuleReference reference = null; + ModuleReference reference; for (int i = 0; i < modules.Count; i++) { - var module_reference = modules [i]; - if (module_reference.Name != file_name) - continue; - - reference = module_reference; - break; + reference = modules [i]; + if (reference.Name == file_name) + return reference; } + reference = new ModuleReference (file_name); + modules.Add (reference); return reference; } @@ -2532,9 +2703,10 @@ namespace Mono.Cecil { GenericParameter GetGenericParameter (GenericParameterType type, uint var) { var context = reader.context; + int index = (int) var; if (context == null) - throw new NotSupportedException (); + return GetUnboundGenericParameter (type, index); IGenericParameterProvider provider; @@ -2549,14 +2721,20 @@ namespace Mono.Cecil { throw new NotSupportedException (); } - int index = (int) var; - if (!context.IsDefinition) CheckGenericContext (provider, index); + if (index >= provider.GenericParameters.Count) + return GetUnboundGenericParameter (type, index); + return provider.GenericParameters [index]; } + GenericParameter GetUnboundGenericParameter (GenericParameterType type, int index) + { + return new GenericParameter (index, type, reader.module); + } + static void CheckGenericContext (IGenericParameterProvider owner, int index) { var owner_parameters = owner.GenericParameters; @@ -2682,12 +2860,24 @@ namespace Mono.Cecil { public void ReadMethodSignature (IMethodSignature method) { var calling_convention = ReadByte (); + + const byte has_this = 0x20; + const byte explicit_this = 0x40; + + if ((calling_convention & has_this) != 0) { + method.HasThis = true; + calling_convention = (byte) (calling_convention & ~has_this); + } + + if ((calling_convention & explicit_this) != 0) { + method.ExplicitThis = true; + calling_convention = (byte) (calling_convention & ~explicit_this); + } + method.CallingConvention = (MethodCallingConvention) calling_convention; - method.HasThis = (calling_convention & 0x20) != 0; - method.ExplicitThis = (calling_convention & 0x40) != 0; var generic_context = method as MethodReference; - if (generic_context != null) + if (generic_context != null && !generic_context.DeclaringType.IsArray) reader.context = generic_context; if ((calling_convention & 0x10) != 0) { @@ -2697,8 +2887,6 @@ namespace Mono.Cecil { CheckGenericContext (generic_context, (int) arity -1 ); } - // TODO: more call_conv - var param_count = ReadCompressedUInt32 (); method.MethodReturnType.ReturnType = ReadTypeSignature (); @@ -2803,10 +2991,11 @@ namespace Mono.Cecil { if (type.IsArray) return ReadCustomAttributeFixedArrayArgument ((ArrayType) type); - if (type.etype == ElementType.Object) - return ReadCustomAttributeElement (ReadCustomAttributeFieldOrPropType ()); - - return new CustomAttributeArgument (type, ReadCustomAttributeElementValue (type)); + return new CustomAttributeArgument ( + type, + type.etype == ElementType.Object + ? ReadCustomAttributeElement (ReadCustomAttributeFieldOrPropType ()) + : ReadCustomAttributeElementValue (type)); } object ReadCustomAttributeElementValue (TypeReference type) diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs index 7aad247..bffa439 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -97,20 +97,18 @@ namespace Mono.Cecil { var symbol_writer = GetSymbolWriter (module, fq_name, symbol_writer_provider); #if !SILVERLIGHT && !CF - if (parameters.StrongNameKeyPair != null && name != null) + if (parameters.StrongNameKeyPair != null && name != null) { name.PublicKey = parameters.StrongNameKeyPair.PublicKey; -#endif - - if (name != null && name.HasPublicKey) module.Attributes |= ModuleAttributes.StrongNameSigned; - + } +#endif var metadata = new MetadataBuilder (module, fq_name, symbol_writer_provider, symbol_writer); BuildMetadata (module, metadata); - if (module.SymbolReader != null) - module.SymbolReader.Dispose (); + if (module.symbol_reader != null) + module.symbol_reader.Dispose (); var writer = ImageWriter.CreateWriter (module, metadata, stream); @@ -786,7 +784,7 @@ namespace Mono.Cecil { TextMap CreateTextMap () { var map = new TextMap (); - map.AddMap (TextSegment.ImportAddressTable, module.Architecture == TargetArchitecture.I386 ? 8 : 16); + map.AddMap (TextSegment.ImportAddressTable, module.Architecture == TargetArchitecture.I386 ? 8 : 0); map.AddMap (TextSegment.CLIHeader, 0x48, 8); return map; } @@ -935,11 +933,13 @@ namespace Mono.Cecil { ? reference.PublicKeyToken : reference.PublicKey; + var version = reference.Version; + var rid = table.AddRow (new AssemblyRefRow ( - (ushort) reference.Version.Major, - (ushort) reference.Version.Minor, - (ushort) reference.Version.Build, - (ushort) reference.Version.Revision, + (ushort) version.Major, + (ushort) version.Minor, + (ushort) version.Build, + (ushort) version.Revision, reference.Attributes, GetBlobIndex (key_or_token), GetStringIndex (reference.Name), @@ -1456,7 +1456,7 @@ namespace Mono.Cecil { { var pinvoke = method.PInvokeInfo; if (pinvoke == null) - throw new ArgumentException (); + return; var table = GetTable<ImplMapTable> (Table.ImplMap); table.AddRow (new ImplMapRow ( @@ -1932,21 +1932,13 @@ namespace Mono.Cecil { SignatureWriter GetSecurityDeclarationSignature (SecurityDeclaration declaration) { var signature = CreateSignatureWriter (); - if (!declaration.resolved) { - signature.WriteBytes (declaration.GetBlob ()); - return signature; - } - - signature.WriteByte ((byte) '.'); - var attributes = declaration.security_attributes; - if (attributes == null) - throw new NotSupportedException (); - - signature.WriteCompressedUInt32 ((uint) attributes.Count); - - for (int i = 0; i < attributes.Count; i++) - signature.WriteSecurityAttribute (attributes [i]); + if (!declaration.resolved) + signature.WriteBytes (declaration.GetBlob ()); + else if (module.Runtime < TargetRuntime.Net_2_0) + signature.WriteXmlSecurityDeclaration (declaration); + else + signature.WriteSecurityDeclaration (declaration); return signature; } @@ -2279,8 +2271,11 @@ namespace Mono.Cecil { } if (type.etype == ElementType.Object) { - WriteCustomAttributeFieldOrPropType (argument.Type); - WriteCustomAttributeElement (argument.Type, argument); + argument = (CustomAttributeArgument) argument.Value; + type = argument.Type; + + WriteCustomAttributeFieldOrPropType (type); + WriteCustomAttributeElement (type, argument); return; } @@ -2446,19 +2441,19 @@ namespace Mono.Cecil { WriteCustomAttributeFixedArgument (argument.Type, argument); } - public void WriteSecurityAttribute (SecurityAttribute attribute) + void WriteSecurityAttribute (SecurityAttribute attribute) { WriteTypeReference (attribute.AttributeType); var count = GetNamedArgumentCount (attribute); if (count == 0) { - WriteCompressedUInt32 (0); // length + WriteCompressedUInt32 (1); // length WriteCompressedUInt32 (0); // count return; } - var buffer = new SignatureWriter (metadata); + var buffer = new SignatureWriter (metadata); buffer.WriteCompressedUInt32 ((uint) count); buffer.WriteICustomAttributeNamedArguments (attribute); @@ -2466,6 +2461,49 @@ namespace Mono.Cecil { WriteBytes (buffer); } + public void WriteSecurityDeclaration (SecurityDeclaration declaration) + { + WriteByte ((byte) '.'); + + var attributes = declaration.security_attributes; + if (attributes == null) + throw new NotSupportedException (); + + WriteCompressedUInt32 ((uint) attributes.Count); + + for (int i = 0; i < attributes.Count; i++) + WriteSecurityAttribute (attributes [i]); + } + + public void WriteXmlSecurityDeclaration (SecurityDeclaration declaration) + { + var xml = GetXmlSecurityDeclaration (declaration); + if (xml == null) + throw new NotSupportedException (); + + WriteBytes (Encoding.Unicode.GetBytes (xml)); + } + + static string GetXmlSecurityDeclaration (SecurityDeclaration declaration) + { + if (declaration.security_attributes == null || declaration.security_attributes.Count != 1) + return null; + + var attribute = declaration.security_attributes [0]; + + if (!attribute.AttributeType.IsTypeOf ("System.Security.Permissions", "PermissionSetAttribute")) + return null; + + if (attribute.properties == null || attribute.properties.Count != 1) + return null; + + var property = attribute.properties [0]; + if (property.Name != "XML") + return null; + + return (string) property.Argument.Value; + } + void WriteTypeReference (TypeReference type) { WriteUTF8String (TypeParser.ToParseable (type)); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs index e449a03..10ab2c3 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -51,6 +51,33 @@ namespace Mono.Cecil { } } +#if !SILVERLIGHT && !CF + [Serializable] +#endif + public class AssemblyResolutionException : FileNotFoundException { + + readonly AssemblyNameReference reference; + + public AssemblyNameReference AssemblyReference { + get { return reference; } + } + + public AssemblyResolutionException (AssemblyNameReference reference) + : base (string.Format ("Failed to resolve assembly: '{0}'", reference)) + { + this.reference = reference; + } + +#if !SILVERLIGHT && !CF + protected AssemblyResolutionException ( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) + : base (info, context) + { + } +#endif + } + public abstract class BaseAssemblyResolver : IAssemblyResolver { static readonly bool on_mono = Type.GetType ("Mono.Runtime") != null; @@ -152,7 +179,7 @@ namespace Mono.Cecil { return assembly; } - throw new FileNotFoundException ("Could not resolve: " + name); + throw new AssemblyResolutionException (name); } AssemblyDefinition SearchDirectory (AssemblyNameReference name, IEnumerable<string> directories, ReaderParameters parameters) @@ -318,10 +345,11 @@ namespace Mono.Cecil { static string GetAssemblyFile (AssemblyNameReference reference, string prefix, string gac) { - var gac_folder = new StringBuilder (); - gac_folder.Append (prefix); - gac_folder.Append (reference.Version); - gac_folder.Append ("__"); + var gac_folder = new StringBuilder () + .Append (prefix) + .Append (reference.Version) + .Append ("__"); + for (int i = 0; i < reference.PublicKeyToken.Length; i++) gac_folder.Append (reference.PublicKeyToken [i].ToString ("x2")); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CallSite.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CallSite.cs index e4880ec..2d4ed41 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CallSite.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CallSite.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -29,11 +29,70 @@ using System; using System.Text; +using Mono.Collections.Generic; + namespace Mono.Cecil { - public sealed class CallSite : MethodReference { + public sealed class CallSite : IMethodSignature { + + readonly MethodReference signature; - public override string FullName { + public bool HasThis { + get { return signature.HasThis; } + set { signature.HasThis = value; } + } + + public bool ExplicitThis { + get { return signature.ExplicitThis; } + set { signature.ExplicitThis = value; } + } + + public MethodCallingConvention CallingConvention { + get { return signature.CallingConvention; } + set { signature.CallingConvention = value; } + } + + public bool HasParameters { + get { return signature.HasParameters; } + } + + public Collection<ParameterDefinition> Parameters { + get { return signature.Parameters; } + } + + public TypeReference ReturnType { + get { return signature.MethodReturnType.ReturnType; } + set { signature.MethodReturnType.ReturnType = value; } + } + + public MethodReturnType MethodReturnType { + get { return signature.MethodReturnType; } + } + + public string Name { + get { return string.Empty; } + set { throw new InvalidOperationException (); } + } + + public string Namespace { + get { return string.Empty; } + set { throw new InvalidOperationException (); } + } + + public ModuleDefinition Module { + get { return ReturnType.Module; } + } + + public IMetadataScope Scope { + get { return signature.ReturnType.Scope; } + } + + public MetadataToken MetadataToken { + get { return signature.token; } + set { signature.token = value; } + } + + public string FullName { get { var signature = new StringBuilder (); signature.Append (ReturnType.FullName); @@ -41,5 +100,25 @@ namespace Mono.Cecil { return signature.ToString (); } } + + internal CallSite () + { + this.signature = new MethodReference (); + this.signature.token = new MetadataToken (TokenType.Signature, 0); + } + + public CallSite (TypeReference returnType) + : this () + { + if (returnType == null) + throw new ArgumentNullException ("returnType"); + + this.signature.ReturnType = returnType; + } + + public override string ToString () + { + return FullName; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CustomAttribute.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CustomAttribute.cs index af47d8b..76372db 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CustomAttribute.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/CustomAttribute.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -103,6 +103,10 @@ namespace Mono.Cecil { get { return constructor.DeclaringType; } } + public bool IsResolved { + get { return resolved; } + } + public bool HasConstructorArguments { get { Resolve (); @@ -184,7 +188,7 @@ namespace Mono.Cecil { if (blob != null) return blob; - if (!HasImage || signature == 0) + if (!HasImage) throw new NotSupportedException (); return blob = Module.Read (this, (attribute, reader) => reader.ReadCustomAttributeBlob (attribute.signature)); @@ -195,12 +199,23 @@ namespace Mono.Cecil { if (resolved || !HasImage) return; - Module.Read (this, (attribute, reader) => { - reader.ReadCustomAttributeSignature (attribute); - return this; - }); - - resolved = true; + try { + Module.Read (this, (attribute, reader) => { + reader.ReadCustomAttributeSignature (attribute); + return this; + }); + + resolved = true; + } catch (ResolutionException) { + if (arguments != null) + arguments.Clear (); + if (fields != null) + fields.Clear (); + if (properties != null) + properties.Clear (); + + resolved = false; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs index cac7974..e0baedf 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -31,18 +31,13 @@ using System.Collections.Generic; namespace Mono.Cecil { - public static class GlobalAssemblyResolver { - - public static readonly IAssemblyResolver Instance = new DefaultAssemblyResolver (); - } - public class DefaultAssemblyResolver : BaseAssemblyResolver { readonly IDictionary<string, AssemblyDefinition> cache; public DefaultAssemblyResolver () { - cache = new Dictionary<string, AssemblyDefinition> (); + cache = new Dictionary<string, AssemblyDefinition> (StringComparer.Ordinal); } public override AssemblyDefinition Resolve (AssemblyNameReference name) diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs index bb9f1f9..e12dd0b 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventAttributes.cs index b5b097d..815efa5 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventDefinition.cs index 7a80aa0..89b5548 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -124,8 +124,8 @@ namespace Mono.Cecil { } public bool IsRuntimeSpecialName { - get { return attributes.GetAttributes ((ushort) FieldAttributes.RTSpecialName); } - set { attributes = attributes.SetAttributes ((ushort) FieldAttributes.RTSpecialName, value); } + get { return attributes.GetAttributes ((ushort) EventAttributes.RTSpecialName); } + set { attributes = attributes.SetAttributes ((ushort) EventAttributes.RTSpecialName, value); } } #endregion @@ -159,5 +159,10 @@ namespace Mono.Cecil { module.Read (this, (@event, reader) => reader.ReadMethods (@event)); } + + public override EventDefinition Resolve () + { + return this; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventReference.cs index e1435da..8952002 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -51,5 +51,7 @@ namespace Mono.Cecil { event_type = eventType; } + + public abstract EventDefinition Resolve (); } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ExportedType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ExportedType.cs index a1e793a..c25f8d0 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ExportedType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ExportedType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -36,6 +36,7 @@ namespace Mono.Cecil { string name; uint attributes; IMetadataScope scope; + ModuleDefinition module; int identifier; ExportedType declaring_type; internal MetadataToken token; @@ -220,16 +221,29 @@ namespace Mono.Cecil { } } - public ExportedType (string @namespace, string name, IMetadataScope scope) + public ExportedType (string @namespace, string name, ModuleDefinition module, IMetadataScope scope) { this.@namespace = @namespace; this.name = name; this.scope = scope; + this.module = module; } public override string ToString () { return FullName; } + + public TypeDefinition Resolve () + { + return module.Resolve (CreateReference ()); + } + + internal TypeReference CreateReference () + { + return new TypeReference (@namespace, name, module, scope) { + DeclaringType = declaring_type != null ? declaring_type.CreateReference () : null, + }; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldAttributes.cs index 947a877..dd6bf36 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldDefinition.cs index c60a940..29a1bc1 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldReference.cs index 1c812bb..be58d3d 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FieldReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FileAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FileAttributes.cs index 6c2781c..4d3b6ca 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FileAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FileAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs index f7bf08c..756d31f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -85,6 +85,7 @@ namespace Mono.Cecil { public override IMetadataScope Scope { get { return function.ReturnType.Scope; } + set { throw new InvalidOperationException (); } } public override bool IsFunctionPointer { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceMethod.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceMethod.cs index d4871c3..dbe720d 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceMethod.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceMethod.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -42,12 +42,7 @@ namespace Mono.Cecil { } public Collection<TypeReference> GenericArguments { - get { - if (arguments == null) - arguments = new Collection<TypeReference> (); - - return arguments; - } + get { return arguments ?? (arguments = new Collection<TypeReference> ()); } } public override bool IsGenericInstance { @@ -70,11 +65,11 @@ namespace Mono.Cecil { get { var signature = new StringBuilder (); var method = this.ElementMethod; - signature.Append (method.ReturnType.FullName); - signature.Append (" "); - signature.Append (method.DeclaringType.FullName); - signature.Append ("::"); - signature.Append (method.Name); + signature.Append (method.ReturnType.FullName) + .Append (" ") + .Append (method.DeclaringType.FullName) + .Append ("::") + .Append (method.Name); this.GenericInstanceFullName (signature); this.MethodSignatureFullName (signature); return signature.ToString (); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs index 46c048b..6554ff0 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -44,12 +44,7 @@ namespace Mono.Cecil { } public Collection<TypeReference> GenericArguments { - get { - if (arguments == null) - arguments = new Collection<TypeReference> (); - - return arguments; - } + get { return arguments ?? (arguments = new Collection<TypeReference> ()); } } public override TypeReference DeclaringType { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs index d3e5a15..d66cc91 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -36,7 +36,9 @@ namespace Mono.Cecil { public sealed class GenericParameter : TypeReference, ICustomAttributeProvider { - readonly IGenericParameterProvider owner; + internal int position; + internal GenericParameterType type; + internal IGenericParameterProvider owner; ushort attributes; Collection<TypeReference> constraints; @@ -48,12 +50,11 @@ namespace Mono.Cecil { } public int Position { - get { - if (owner == null) - return -1; + get { return position; } + } - return owner.GenericParameters.IndexOf (this); - } + public GenericParameterType Type { + get { return type; } } public IGenericParameterProvider Owner { @@ -97,21 +98,29 @@ namespace Mono.Cecil { get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } } - internal new bool HasImage { - get { return Module != null && Module.HasImage; } - } - public override IMetadataScope Scope { get { - if (owner.GenericParameterType == GenericParameterType.Method) - return ((MethodReference) owner).DeclaringType.Scope; + if (owner == null) + return null; - return ((TypeReference) owner).Scope; + return owner.GenericParameterType == GenericParameterType.Method + ? ((MethodReference) owner).DeclaringType.Scope + : ((TypeReference) owner).Scope; } + set { throw new InvalidOperationException (); } + } + + public override TypeReference DeclaringType { + get { return owner as TypeReference; } + set { throw new InvalidOperationException (); } + } + + public MethodReference DeclaringMethod { + get { return owner as MethodReference; } } public override ModuleDefinition Module { - get { return ((MemberReference) owner).Module; } + get { return module ?? owner.Module; } } public override string Name { @@ -119,7 +128,7 @@ namespace Mono.Cecil { if (!string.IsNullOrEmpty (base.Name)) return base.Name; - return base.Name = (owner.GenericParameterType == GenericParameterType.Type ? "!" : "!!") + Position; + return base.Name = (type == GenericParameterType.Method ? "!!" : "!") + position; } } @@ -189,8 +198,34 @@ namespace Mono.Cecil { if (owner == null) throw new ArgumentNullException (); + this.position = -1; this.owner = owner; - this.etype = owner.GenericParameterType == GenericParameterType.Type ? ElementType.Var : ElementType.MVar; + this.type = owner.GenericParameterType; + this.etype = ConvertGenericParameterType (this.type); + } + + public GenericParameter (int position, GenericParameterType type, ModuleDefinition module) + : base (string.Empty, string.Empty) + { + if (module == null) + throw new ArgumentNullException (); + + this.position = position; + this.type = type; + this.etype = ConvertGenericParameterType (type); + this.module = module; + } + + static ElementType ConvertGenericParameterType (GenericParameterType type) + { + switch (type) { + case GenericParameterType.Type: + return ElementType.Var; + case GenericParameterType.Method: + return ElementType.MVar; + } + + throw new ArgumentOutOfRangeException (); } public override TypeDefinition Resolve () @@ -198,4 +233,55 @@ namespace Mono.Cecil { return null; } } + + sealed class GenericParameterCollection : Collection<GenericParameter> { + + readonly IGenericParameterProvider owner; + + internal GenericParameterCollection (IGenericParameterProvider owner) + { + this.owner = owner; + } + + internal GenericParameterCollection (IGenericParameterProvider owner, int capacity) + : base (capacity) + { + this.owner = owner; + } + + protected override void OnAdd (GenericParameter item, int index) + { + UpdateGenericParameter (item, index); + } + + protected override void OnInsert (GenericParameter item, int index) + { + UpdateGenericParameter (item, index); + + for (int i = index; i < size; i++) + items[i].position = i + 1; + } + + protected override void OnSet (GenericParameter item, int index) + { + UpdateGenericParameter (item, index); + } + + void UpdateGenericParameter (GenericParameter item, int index) + { + item.owner = owner; + item.position = index; + item.type = owner.GenericParameterType; + } + + protected override void OnRemove (GenericParameter item, int index) + { + item.owner = null; + item.position = -1; + item.type = GenericParameterType.Type; + + for (int i = index + 1; i < size; i++) + items[i].position = i - 1; + } + } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameterAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameterAttributes.cs index 8999614..6d77956 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameterAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameterAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs index e3338cf..8bcd7d7 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs index 08e5f92..916ba0f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -45,9 +45,7 @@ namespace Mono.Cecil { this ICustomAttributeProvider self, ModuleDefinition module) { - return module.HasImage () - ? module.Read (self, (provider, reader) => reader.HasCustomAttributes (provider)) - : false; + return module.HasImage () && module.Read (self, (provider, reader) => reader.HasCustomAttributes (provider)); } public static Collection<CustomAttribute> GetCustomAttributes ( diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericInstance.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericInstance.cs index 2750ad0..edc406c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericInstance.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericInstance.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs index 27d1669..d41b7f6 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -58,9 +58,7 @@ namespace Mono.Cecil { this IGenericParameterProvider self, ModuleDefinition module) { - return module.HasImage () - ? module.Read (self, (provider, reader) => reader.HasGenericParameters (provider)) - : false; + return module.HasImage () && module.Read (self, (provider, reader) => reader.HasGenericParameters (provider)); } public static Collection<GenericParameter> GetGenericParameters ( @@ -69,7 +67,7 @@ namespace Mono.Cecil { { return module.HasImage () ? module.Read (self, (provider, reader) => reader.ReadGenericParameters (provider)) - : new Collection<GenericParameter> (); + : new GenericParameterCollection (self); } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs index a3bba6d..67a53a3 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -40,9 +40,7 @@ namespace Mono.Cecil { this IMarshalInfoProvider self, ModuleDefinition module) { - return module.HasImage () - ? module.Read (self, (provider, reader) => reader.HasMarshalInfo (provider)) - : false; + return module.HasImage () && module.Read (self, (provider, reader) => reader.HasMarshalInfo (provider)); } public static MarshalInfo GetMarshalInfo ( diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMemberDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMemberDefinition.cs index 09878f7..ee73d9d 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMemberDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMemberDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataScope.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataScope.cs index f0ab506..04a0228 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataScope.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataScope.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataTokenProvider.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataTokenProvider.cs index ff77724..6621835 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataTokenProvider.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMetadataTokenProvider.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMethodSignature.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMethodSignature.cs index 6bae56a..e3d288b 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMethodSignature.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IMethodSignature.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -46,6 +46,11 @@ namespace Mono.Cecil { static partial class Mixin { + public static bool HasImplicitThis (this IMethodSignature self) + { + return self.HasThis && !self.ExplicitThis; + } + public static void MethodSignatureFullName (this IMethodSignature self, StringBuilder builder) { builder.Append ("("); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs index 93e700e..a32eb3c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -28,6 +28,7 @@ using System; using System.Collections.Generic; +using Mono.Collections.Generic; using SR = System.Reflection; using Mono.Cecil.Metadata; @@ -39,6 +40,76 @@ namespace Mono.Cecil { Open, } + struct ImportGenericContext { + + Collection<IGenericParameterProvider> stack; + + public bool IsEmpty { get { return stack == null; } } + + public ImportGenericContext (IGenericParameterProvider provider) + { + stack = null; + + Push (provider); + } + + public void Push (IGenericParameterProvider provider) + { + if (stack == null) + stack = new Collection<IGenericParameterProvider> (1) { provider }; + else + stack.Add (provider); + } + + public void Pop () + { + stack.RemoveAt (stack.Count - 1); + } + + public TypeReference MethodParameter (string method, int position) + { + for (int i = stack.Count - 1; i >= 0; i--) { + var candidate = stack [i] as MethodReference; + if (candidate == null) + continue; + + if (method != candidate.Name) + continue; + + return candidate.GenericParameters [position]; + } + + throw new InvalidOperationException (); + } + + public TypeReference TypeParameter (string type, int position) + { + for (int i = stack.Count - 1; i >= 0; i--) { + var candidate = GenericTypeFor (stack [i]); + + if (candidate.FullName != type) + continue; + + return candidate.GenericParameters [position]; + } + + throw new InvalidOperationException (); + } + + static TypeReference GenericTypeFor (IGenericParameterProvider context) + { + var type = context as TypeReference; + if (type != null) + return type.GetElementType (); + + var method = context as MethodReference; + if (method != null) + return method.DeclaringType.GetElementType (); + + throw new InvalidOperationException (); + } + } + class MetadataImporter { readonly ModuleDefinition module; @@ -70,12 +141,12 @@ namespace Mono.Cecil { { typeof (object), ElementType.Object }, }; - public TypeReference ImportType (Type type, IGenericContext context) + public TypeReference ImportType (Type type, ImportGenericContext context) { return ImportType (type, context, ImportGenericKind.Open); } - public TypeReference ImportType (Type type, IGenericContext context, ImportGenericKind import_kind) + public TypeReference ImportType (Type type, ImportGenericContext context, ImportGenericKind import_kind) { if (IsTypeSpecification (type) || ImportOpenGenericType (type, import_kind)) return ImportTypeSpecification (type, context); @@ -92,7 +163,7 @@ namespace Mono.Cecil { if (IsNestedType (type)) reference.DeclaringType = ImportType (type.DeclaringType, context, import_kind); else - reference.Namespace = type.Namespace; + reference.Namespace = type.Namespace ?? string.Empty; if (type.IsGenericType) ImportGenericParameters (reference, type.GetGenericArguments ()); @@ -119,7 +190,7 @@ namespace Mono.Cecil { #endif } - TypeReference ImportTypeSpecification (Type type, IGenericContext context) + TypeReference ImportTypeSpecification (Type type, ImportGenericContext context) { if (type.IsByRef) return new ByReferenceType (ImportType (type.GetElementType (), context)); @@ -139,32 +210,44 @@ namespace Mono.Cecil { throw new NotSupportedException (type.FullName); } - static TypeReference ImportGenericParameter (Type type, IGenericContext context) + static TypeReference ImportGenericParameter (Type type, ImportGenericContext context) { - if (context == null) + if (context.IsEmpty) throw new InvalidOperationException (); - var owner = type.DeclaringMethod != null - ? context.Method - : context.Type; + if (type.DeclaringMethod != null) + return context.MethodParameter (type.DeclaringMethod.Name, type.GenericParameterPosition); - if (owner == null) - throw new InvalidOperationException (); + if (type.DeclaringType != null) + return context.TypeParameter (NormalizedFullName (type.DeclaringType), type.GenericParameterPosition); + + throw new InvalidOperationException(); + } + + private static string NormalizedFullName (Type type) + { + if (IsNestedType (type)) + return NormalizedFullName (type.DeclaringType) + "/" + type.Name; - return owner.GenericParameters [type.GenericParameterPosition]; + return type.FullName; } - TypeReference ImportGenericInstance (Type type, IGenericContext context) + TypeReference ImportGenericInstance (Type type, ImportGenericContext context) { var element_type = ImportType (type.GetGenericTypeDefinition (), context, ImportGenericKind.Definition); var instance = new GenericInstanceType (element_type); var arguments = type.GetGenericArguments (); var instance_arguments = instance.GenericArguments; - for (int i = 0; i < arguments.Length; i++) - instance_arguments.Add (ImportType (arguments [i], context ?? element_type)); + context.Push (element_type); + try { + for (int i = 0; i < arguments.Length; i++) + instance_arguments.Add (ImportType (arguments [i], context)); - return instance; + return instance; + } finally { + context.Pop (); + } } static bool IsTypeSpecification (Type type) @@ -237,18 +320,23 @@ namespace Mono.Cecil { } #endif - public FieldReference ImportField (SR.FieldInfo field, IGenericContext context) + public FieldReference ImportField (SR.FieldInfo field, ImportGenericContext context) { var declaring_type = ImportType (field.DeclaringType, context); if (IsGenericInstance (field.DeclaringType)) field = ResolveFieldDefinition (field); - return new FieldReference { - Name = field.Name, - DeclaringType = declaring_type, - FieldType = ImportType (field.FieldType, context ?? declaring_type), - }; + context.Push (declaring_type); + try { + return new FieldReference { + Name = field.Name, + DeclaringType = declaring_type, + FieldType = ImportType (field.FieldType, context), + }; + } finally { + context.Pop (); + } } static SR.FieldInfo ResolveFieldDefinition (SR.FieldInfo field) @@ -263,7 +351,7 @@ namespace Mono.Cecil { #endif } - public MethodReference ImportMethod (SR.MethodBase method, IGenericContext context, ImportGenericKind import_kind) + public MethodReference ImportMethod (SR.MethodBase method, ImportGenericContext context, ImportGenericKind import_kind) { if (IsMethodSpecification (method) || ImportOpenGenericMethod (method, import_kind)) return ImportMethodSpecification (method, context); @@ -286,21 +374,26 @@ namespace Mono.Cecil { if (method.IsGenericMethod) ImportGenericParameters (reference, method.GetGenericArguments ()); - var method_info = method as SR.MethodInfo; - reference.ReturnType = method_info != null - ? ImportType (method_info.ReturnType, context ?? reference) - : ImportType (typeof (void), null); + context.Push (reference); + try { + var method_info = method as SR.MethodInfo; + reference.ReturnType = method_info != null + ? ImportType (method_info.ReturnType, context) + : ImportType (typeof (void), default (ImportGenericContext)); - var parameters = method.GetParameters (); - var reference_parameters = reference.Parameters; + var parameters = method.GetParameters (); + var reference_parameters = reference.Parameters; - for (int i = 0; i < parameters.Length; i++) - reference_parameters.Add ( - new ParameterDefinition (ImportType (parameters [i].ParameterType, context ?? reference))); + for (int i = 0; i < parameters.Length; i++) + reference_parameters.Add ( + new ParameterDefinition (ImportType (parameters [i].ParameterType, context))); - reference.DeclaringType = declaring_type; + reference.DeclaringType = declaring_type; - return reference; + return reference; + } finally { + context.Pop (); + } } static void ImportGenericParameters (IGenericParameterProvider provider, Type [] arguments) @@ -316,7 +409,7 @@ namespace Mono.Cecil { return method.IsGenericMethod && !method.IsGenericMethodDefinition; } - MethodReference ImportMethodSpecification (SR.MethodBase method, IGenericContext context) + MethodReference ImportMethodSpecification (SR.MethodBase method, ImportGenericContext context) { var method_info = method as SR.MethodInfo; if (method_info == null) @@ -327,10 +420,15 @@ namespace Mono.Cecil { var arguments = method.GetGenericArguments (); var instance_arguments = instance.GenericArguments; - for (int i = 0; i < arguments.Length; i++) - instance_arguments.Add (ImportType (arguments [i], context ?? element_method)); + context.Push (element_method); + try { + for (int i = 0; i < arguments.Length; i++) + instance_arguments.Add (ImportType (arguments [i], context)); - return instance; + return instance; + } finally { + context.Pop (); + } } static bool HasCallingConvention (SR.MethodBase method, SR.CallingConventions conventions) @@ -339,7 +437,7 @@ namespace Mono.Cecil { } #endif - public TypeReference ImportType (TypeReference type, IGenericContext context) + public TypeReference ImportType (TypeReference type, ImportGenericContext context) { if (type.IsTypeSpecification ()) return ImportTypeSpecification (type, context); @@ -351,7 +449,7 @@ namespace Mono.Cecil { ImportScope (type.Scope), type.IsValueType); - MetadataSystem.TryProcessPrimitiveType (reference); + MetadataSystem.TryProcessPrimitiveTypeReference (reference); if (type.IsNested) reference.DeclaringType = ImportType (type.DeclaringType, context); @@ -427,7 +525,7 @@ namespace Mono.Cecil { imported_parameters.Add (new GenericParameter (parameters [i].Name, imported)); } - TypeReference ImportTypeSpecification (TypeReference type, IGenericContext context) + TypeReference ImportTypeSpecification (TypeReference type, ImportGenericContext context) { switch (type.etype) { case ElementType.SzArray: @@ -486,32 +584,33 @@ namespace Mono.Cecil { return imported_instance; case ElementType.Var: - if (context == null || context.Type == null) - throw new InvalidOperationException (); - - return ((TypeReference) context.Type).GetElementType ().GenericParameters [((GenericParameter) type).Position]; + var var_parameter = (GenericParameter) type; + return context.TypeParameter (type.DeclaringType.FullName, var_parameter.Position); case ElementType.MVar: - if (context == null || context.Method == null) - throw new InvalidOperationException (); - - return context.Method.GenericParameters [((GenericParameter) type).Position]; + var mvar_parameter = (GenericParameter) type; + return context.MethodParameter (mvar_parameter.DeclaringMethod.Name, mvar_parameter.Position); } throw new NotSupportedException (type.etype.ToString ()); } - public FieldReference ImportField (FieldReference field, IGenericContext context) + public FieldReference ImportField (FieldReference field, ImportGenericContext context) { var declaring_type = ImportType (field.DeclaringType, context); - return new FieldReference { - Name = field.Name, - DeclaringType = declaring_type, - FieldType = ImportType (field.FieldType, context ?? declaring_type), - }; + context.Push (declaring_type); + try { + return new FieldReference { + Name = field.Name, + DeclaringType = declaring_type, + FieldType = ImportType (field.FieldType, context), + }; + } finally { + context.Pop (); + } } - public MethodReference ImportMethod (MethodReference method, IGenericContext context) + public MethodReference ImportMethod (MethodReference method, ImportGenericContext context) { if (method.IsGenericInstance) return ImportMethodSpecification (method, context); @@ -523,29 +622,33 @@ namespace Mono.Cecil { HasThis = method.HasThis, ExplicitThis = method.ExplicitThis, DeclaringType = declaring_type, + CallingConvention = method.CallingConvention, }; - reference.CallingConvention = method.CallingConvention; - if (method.HasGenericParameters) ImportGenericParameters (reference, method); - reference.ReturnType = ImportType (method.ReturnType, context ?? reference); + context.Push (reference); + try { + reference.ReturnType = ImportType (method.ReturnType, context); - if (!method.HasParameters) - return reference; + if (!method.HasParameters) + return reference; - var reference_parameters = reference.Parameters; + var reference_parameters = reference.Parameters; - var parameters = method.Parameters; - for (int i = 0; i < parameters.Count; i++) - reference_parameters.Add ( - new ParameterDefinition (ImportType (parameters [i].ParameterType, context ?? reference))); + var parameters = method.Parameters; + for (int i = 0; i < parameters.Count; i++) + reference_parameters.Add ( + new ParameterDefinition (ImportType (parameters [i].ParameterType, context))); - return reference; + return reference; + } finally { + context.Pop(); + } } - MethodSpecification ImportMethodSpecification (MethodReference method, IGenericContext context) + MethodSpecification ImportMethodSpecification (MethodReference method, ImportGenericContext context) { if (!method.IsGenericInstance) throw new NotSupportedException (); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/LinkedResource.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/LinkedResource.cs index a5530fc..16c1d59 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/LinkedResource.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/LinkedResource.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ManifestResourceAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ManifestResourceAttributes.cs index d72d1d0..7d6bb19 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ManifestResourceAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ManifestResourceAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MarshalInfo.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MarshalInfo.cs index 1fce23a..9d58738 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MarshalInfo.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MarshalInfo.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberDefinitionCollection.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberDefinitionCollection.cs index 1e70302..707f36f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberDefinitionCollection.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberDefinitionCollection.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberReference.cs index b3edf39..b658c51 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MemberReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs index 464fb94..e69fcd7 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -27,7 +27,6 @@ // using System; -using System.Collections.Generic; using Mono.Collections.Generic; @@ -41,16 +40,66 @@ namespace Mono.Cecil { AssemblyDefinition Resolve (string fullName, ReaderParameters parameters); } - static class MetadataResolver { + public interface IMetadataResolver { + TypeDefinition Resolve (TypeReference type); + FieldDefinition Resolve (FieldReference field); + MethodDefinition Resolve (MethodReference method); + } + +#if !SILVERLIGHT && !CF + [Serializable] +#endif + public class ResolutionException : Exception { + + readonly MemberReference member; + + public MemberReference Member { + get { return member; } + } + + public ResolutionException (MemberReference member) + : base ("Failed to resolve " + member.FullName) + { + this.member = member; + } + +#if !SILVERLIGHT && !CF + protected ResolutionException ( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) + : base (info, context) + { + } +#endif + } + + public class MetadataResolver : IMetadataResolver { - public static TypeDefinition Resolve (IAssemblyResolver resolver, TypeReference type) + readonly IAssemblyResolver assembly_resolver; + + public IAssemblyResolver AssemblyResolver { + get { return assembly_resolver; } + } + + public MetadataResolver (IAssemblyResolver assemblyResolver) + { + if (assemblyResolver == null) + throw new ArgumentNullException ("assemblyResolver"); + + assembly_resolver = assemblyResolver; + } + + public virtual TypeDefinition Resolve (TypeReference type) { + if (type == null) + throw new ArgumentNullException ("type"); + type = type.GetElementType (); var scope = type.Scope; switch (scope.MetadataScopeType) { case MetadataScopeType.AssemblyNameReference: - var assembly = resolver.Resolve ((AssemblyNameReference) scope); + var assembly = assembly_resolver.Resolve ((AssemblyNameReference) scope); if (assembly == null) return null; @@ -71,7 +120,32 @@ namespace Mono.Cecil { throw new NotSupportedException (); } - static TypeDefinition GetType (ModuleDefinition module, TypeReference type) + static TypeDefinition GetType (ModuleDefinition module, TypeReference reference) + { + var type = GetTypeDefinition (module, reference); + if (type != null) + return type; + + if (!module.HasExportedTypes) + return null; + + var exported_types = module.ExportedTypes; + + for (int i = 0; i < exported_types.Count; i++) { + var exported_type = exported_types [i]; + if (exported_type.Name != reference.Name) + continue; + + if (exported_type.Namespace != reference.Namespace) + continue; + + return exported_type.Resolve (); + } + + return null; + } + + static TypeDefinition GetTypeDefinition (ModuleDefinition module, TypeReference type) { if (!type.IsNested) return module.GetType (type.Namespace, type.Name); @@ -83,19 +157,22 @@ namespace Mono.Cecil { return declaring_type.GetNestedType (type.Name); } - public static FieldDefinition Resolve (IAssemblyResolver resolver, FieldReference field) + public virtual FieldDefinition Resolve (FieldReference field) { - var type = Resolve (resolver, field.DeclaringType); + if (field == null) + throw new ArgumentNullException ("field"); + + var type = Resolve (field.DeclaringType); if (type == null) return null; if (!type.HasFields) return null; - return GetField (resolver, type, field); + return GetField (type, field); } - static FieldDefinition GetField (IAssemblyResolver resolver, TypeDefinition type, FieldReference reference) + FieldDefinition GetField (TypeDefinition type, FieldReference reference) { while (type != null) { var field = GetField (type.Fields, reference); @@ -105,13 +182,13 @@ namespace Mono.Cecil { if (type.BaseType == null) return null; - type = Resolve (resolver, type.BaseType); + type = Resolve (type.BaseType); } return null; } - static FieldDefinition GetField (IList<FieldDefinition> fields, FieldReference reference) + static FieldDefinition GetField (Collection<FieldDefinition> fields, FieldReference reference) { for (int i = 0; i < fields.Count; i++) { var field = fields [i]; @@ -128,9 +205,12 @@ namespace Mono.Cecil { return null; } - public static MethodDefinition Resolve (IAssemblyResolver resolver, MethodReference method) + public virtual MethodDefinition Resolve (MethodReference method) { - var type = Resolve (resolver, method.DeclaringType); + if (method == null) + throw new ArgumentNullException ("method"); + + var type = Resolve (method.DeclaringType); if (type == null) return null; @@ -139,10 +219,10 @@ namespace Mono.Cecil { if (!type.HasMethods) return null; - return GetMethod (resolver, type, method); + return GetMethod (type, method); } - static MethodDefinition GetMethod (IAssemblyResolver resolver, TypeDefinition type, MethodReference reference) + MethodDefinition GetMethod (TypeDefinition type, MethodReference reference) { while (type != null) { var method = GetMethod (type.Methods, reference); @@ -152,13 +232,13 @@ namespace Mono.Cecil { if (type.BaseType == null) return null; - type = Resolve (resolver, type.BaseType); + type = Resolve (type.BaseType); } return null; } - public static MethodDefinition GetMethod (IList<MethodDefinition> methods, MethodReference reference) + public static MethodDefinition GetMethod (Collection<MethodDefinition> methods, MethodReference reference) { for (int i = 0; i < methods.Count; i++) { var method = methods [i]; @@ -166,6 +246,12 @@ namespace Mono.Cecil { if (method.Name != reference.Name) continue; + if (method.HasGenericParameters != reference.HasGenericParameters) + continue; + + if (method.HasGenericParameters && method.GenericParameters.Count != reference.GenericParameters.Count) + continue; + if (!AreSame (method.ReturnType, reference.ReturnType)) continue; @@ -235,12 +321,6 @@ namespace Mono.Cecil { static bool AreSame (GenericInstanceType a, GenericInstanceType b) { - if (!a.HasGenericArguments) - return !b.HasGenericArguments; - - if (!b.HasGenericArguments) - return false; - if (a.GenericArguments.Count != b.GenericArguments.Count) return false; @@ -258,6 +338,12 @@ namespace Mono.Cecil { static bool AreSame (TypeReference a, TypeReference b) { + if (ReferenceEquals (a, b)) + return true; + + if (a == null || b == null) + return false; + if (a.etype != b.etype) return false; @@ -267,7 +353,12 @@ namespace Mono.Cecil { if (a.IsTypeSpecification ()) return AreSame ((TypeSpecification) a, (TypeSpecification) b); - return a.FullName == b.FullName; + if (a.Name != b.Name || a.Namespace != b.Namespace) + return false; + + //TODO: check scope + + return AreSame (a.DeclaringType, b.DeclaringType); } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs index e2196af..3ae2015 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -46,6 +46,9 @@ namespace Mono.Cecil { sealed class MetadataSystem { + internal AssemblyNameReference [] AssemblyReferences; + internal ModuleReference [] ModuleReferences; + internal TypeDefinition [] Types; internal TypeReference [] TypeReferences; @@ -62,20 +65,20 @@ namespace Mono.Cecil { internal Dictionary<MetadataToken, uint> FieldMarshals; internal Dictionary<MetadataToken, Row<ElementType, uint>> Constants; internal Dictionary<uint, MetadataToken []> Overrides; - internal Dictionary<MetadataToken, Range> CustomAttributes; - internal Dictionary<MetadataToken, Range> SecurityDeclarations; + internal Dictionary<MetadataToken, Range []> CustomAttributes; + internal Dictionary<MetadataToken, Range []> SecurityDeclarations; internal Dictionary<uint, Range> Events; internal Dictionary<uint, Range> Properties; internal Dictionary<uint, Row<MethodSemanticsAttributes, MetadataToken>> Semantics; internal Dictionary<uint, Row<PInvokeAttributes, uint, uint>> PInvokes; - internal Dictionary<MetadataToken, Range> GenericParameters; + internal Dictionary<MetadataToken, Range []> GenericParameters; internal Dictionary<uint, MetadataToken []> GenericConstraints; static Dictionary<string, Row<ElementType, bool>> primitive_value_types; static void InitializePrimitives () { - primitive_value_types = new Dictionary<string, Row<ElementType, bool>> (18) { + primitive_value_types = new Dictionary<string, Row<ElementType, bool>> (18, StringComparer.Ordinal) { { "Void", new Row<ElementType, bool> (ElementType.Void, false) }, { "Boolean", new Row<ElementType, bool> (ElementType.Boolean, true) }, { "Char", new Row<ElementType, bool> (ElementType.Char, true) }, @@ -97,30 +100,45 @@ namespace Mono.Cecil { }; } - public static void TryProcessPrimitiveType (TypeReference type) + public static void TryProcessPrimitiveTypeReference (TypeReference type) { - var scope = type.scope; - if (scope == null) + if (type.Namespace != "System") return; - if (scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference) + var scope = type.scope; + if (scope == null || scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference) return; - if (scope.Name != "mscorlib") + Row<ElementType, bool> primitive_data; + if (!TryGetPrimitiveData (type, out primitive_data)) return; + type.etype = primitive_data.Col1; + type.IsValueType = primitive_data.Col2; + } + + public static bool TryGetPrimitiveElementType (TypeDefinition type, out ElementType etype) + { + etype = ElementType.None; + if (type.Namespace != "System") - return; + return false; + Row<ElementType, bool> primitive_data; + if (TryGetPrimitiveData (type, out primitive_data) && primitive_data.Col1.IsPrimitive ()) { + etype = primitive_data.Col1; + return true; + } + + return false; + } + + static bool TryGetPrimitiveData (TypeReference type, out Row<ElementType, bool> primitive_data) + { if (primitive_value_types == null) InitializePrimitives (); - Row<ElementType, bool> primitive_data; - if (!primitive_value_types.TryGetValue (type.Name, out primitive_data)) - return; - - type.etype = primitive_data.Col1; - type.IsValueType = primitive_data.Col2; + return primitive_value_types.TryGetValue (type.Name, out primitive_data); } public void Clear () @@ -284,9 +302,9 @@ namespace Mono.Cecil { Events.Remove (type.token.RID); } - public bool TryGetGenericParameterRange (IGenericParameterProvider owner, out Range range) + public bool TryGetGenericParameterRanges (IGenericParameterProvider owner, out Range [] ranges) { - return GenericParameters.TryGetValue (owner.MetadataToken, out range); + return GenericParameters.TryGetValue (owner.MetadataToken, out ranges); } public void RemoveGenericParameterRange (IGenericParameterProvider owner) @@ -294,9 +312,9 @@ namespace Mono.Cecil { GenericParameters.Remove (owner.MetadataToken); } - public bool TryGetCustomAttributeRange (ICustomAttributeProvider owner, out Range range) + public bool TryGetCustomAttributeRanges (ICustomAttributeProvider owner, out Range [] ranges) { - return CustomAttributes.TryGetValue (owner.MetadataToken, out range); + return CustomAttributes.TryGetValue (owner.MetadataToken, out ranges); } public void RemoveCustomAttributeRange (ICustomAttributeProvider owner) @@ -304,9 +322,9 @@ namespace Mono.Cecil { CustomAttributes.Remove (owner.MetadataToken); } - public bool TryGetSecurityDeclarationRange (ISecurityDeclarationProvider owner, out Range range) + public bool TryGetSecurityDeclarationRanges (ISecurityDeclarationProvider owner, out Range [] ranges) { - return SecurityDeclarations.TryGetValue (owner.MetadataToken, out range); + return SecurityDeclarations.TryGetValue (owner.MetadataToken, out ranges); } public void RemoveSecurityDeclarationRange (ISecurityDeclarationProvider owner) diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodAttributes.cs index 3446702..626a97b 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodCallingConvention.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodCallingConvention.cs index c96dc1c..bd7188d 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodCallingConvention.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodCallingConvention.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs index 3b74e5a..cbda745 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs index 8a2b422..b24fcf7 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -49,6 +49,5 @@ namespace Mono.Cecil { Synchronized = 0x0020, // Method is single threaded through the body NoOptimization = 0x0040, // Method is not optimized by the JIT. NoInlining = 0x0008, // Method may not be inlined - MaxMethodImplVal = 0xffff // Range check value } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReference.cs index 1ea47a0..0adab45 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -99,7 +99,7 @@ namespace Mono.Cecil { if (generic_parameters != null) return generic_parameters; - return generic_parameters = new Collection<GenericParameter> (); + return generic_parameters = new GenericParameterCollection (this); } } @@ -123,9 +123,9 @@ namespace Mono.Cecil { public override string FullName { get { var builder = new StringBuilder (); - builder.Append (ReturnType.FullName); - builder.Append (" "); - builder.Append (MemberFullName ()); + builder.Append (ReturnType.FullName) + .Append (" ") + .Append (MemberFullName ()); this.MethodSignatureFullName (builder); return builder.ToString (); } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs index 045b9a8..b5b3ee8 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -46,8 +46,7 @@ namespace Mono.Cecil { } internal ParameterDefinition Parameter { - get { return parameter ?? (parameter = new ParameterDefinition (return_type)); } - set { parameter = value; } + get { return parameter ?? (parameter = new ParameterDefinition (return_type, method)); } } public MetadataToken MetadataToken { @@ -55,6 +54,11 @@ namespace Mono.Cecil { set { Parameter.MetadataToken = value; } } + public ParameterAttributes Attributes { + get { return Parameter.Attributes; } + set { Parameter.Attributes = value; } + } + public bool HasCustomAttributes { get { return parameter != null && parameter.HasCustomAttributes; } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSemanticsAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSemanticsAttributes.cs index f44a3fa..dd0f474 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSemanticsAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSemanticsAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSpecification.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSpecification.cs index e907d6c..73b5c14 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSpecification.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodSpecification.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Modifiers.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Modifiers.cs index f7ff611..ad31bc0 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Modifiers.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Modifiers.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs index 7c793f5..7f75cbd 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -47,6 +47,7 @@ namespace Mono.Cecil { ReadingMode reading_mode; IAssemblyResolver assembly_resolver; + IMetadataResolver metadata_resolver; Stream symbol_stream; ISymbolReaderProvider symbol_reader_provider; bool read_symbols; @@ -61,6 +62,11 @@ namespace Mono.Cecil { set { assembly_resolver = value; } } + public IMetadataResolver MetadataResolver { + get { return metadata_resolver; } + set { metadata_resolver = value; } + } + public Stream SymbolStream { get { return symbol_stream; } set { symbol_stream = value; } @@ -95,6 +101,7 @@ namespace Mono.Cecil { TargetRuntime runtime; TargetArchitecture architecture; IAssemblyResolver assembly_resolver; + IMetadataResolver metadata_resolver; public ModuleKind Kind { get { return kind; } @@ -116,6 +123,11 @@ namespace Mono.Cecil { set { assembly_resolver = value; } } + public IMetadataResolver MetadataResolver { + get { return metadata_resolver; } + set { metadata_resolver = value; } + } + public ModuleParameters () { this.kind = ModuleKind.Dll; @@ -183,9 +195,10 @@ namespace Mono.Cecil { internal MetadataSystem MetadataSystem; internal ReadingMode ReadingMode; internal ISymbolReaderProvider SymbolReaderProvider; - internal ISymbolReader SymbolReader; + internal ISymbolReader symbol_reader; internal IAssemblyResolver assembly_resolver; + internal IMetadataResolver metadata_resolver; internal TypeSystem type_system; readonly MetadataReader reader; @@ -195,6 +208,7 @@ namespace Mono.Cecil { TargetRuntime runtime; TargetArchitecture architecture; ModuleAttributes attributes; + ModuleCharacteristics characteristics; Guid mvid; internal AssemblyDefinition assembly; @@ -234,6 +248,11 @@ namespace Mono.Cecil { set { attributes = value; } } + public ModuleCharacteristics Characteristics { + get { return characteristics; } + set { characteristics = value; } + } + public string FullyQualifiedName { get { return fq_name; } } @@ -248,7 +267,11 @@ namespace Mono.Cecil { } public bool HasSymbols { - get { return SymbolReader != null; } + get { return symbol_reader != null; } + } + + public ISymbolReader SymbolReader { + get { return symbol_reader; } } public override MetadataScopeType MetadataScopeType { @@ -266,7 +289,11 @@ namespace Mono.Cecil { #endif public IAssemblyResolver AssemblyResolver { - get { return assembly_resolver; } + get { return assembly_resolver ?? (assembly_resolver = new DefaultAssemblyResolver ()); } + } + + public IMetadataResolver MetadataResolver { + get { return metadata_resolver ?? (metadata_resolver = new MetadataResolver (this.AssemblyResolver)); } } public TypeSystem TypeSystem { @@ -411,7 +438,6 @@ namespace Mono.Cecil { { this.MetadataSystem = new MetadataSystem (); this.token = new MetadataToken (TokenType.Module, 1); - this.assembly_resolver = GlobalAssemblyResolver.Instance; } internal ModuleDefinition (Image image) @@ -422,6 +448,7 @@ namespace Mono.Cecil { this.runtime = image.Runtime; this.architecture = image.Architecture; this.attributes = image.Attributes; + this.characteristics = image.Characteristics; this.fq_name = image.FileName; this.reader = new MetadataReader (this); @@ -439,7 +466,7 @@ namespace Mono.Cecil { if (!HasImage) return false; - return Read (this, (_, reader) => reader.GetTypeReference (scope, fullName) != null); + return GetTypeReference (scope, fullName) != null; } public bool TryGetTypeReference (string fullName, out TypeReference type) @@ -456,7 +483,12 @@ namespace Mono.Cecil { return false; } - return (type = Read (this, (_, reader) => reader.GetTypeReference (scope, fullName))) != null; + return (type = GetTypeReference (scope, fullName)) != null; + } + + TypeReference GetTypeReference (string scope, string fullname) + { + return Read (new Row<string, string> (scope, fullname), (row, reader) => reader.GetTypeReference (row.Col1, row.Col2)); } public IEnumerable<TypeReference> GetTypeReferences () @@ -475,6 +507,13 @@ namespace Mono.Cecil { return Read (this, (_, reader) => reader.GetMemberReferences ()); } + public TypeReference GetType (string fullName, bool runtimeName) + { + return runtimeName + ? TypeParser.ParseType (this, fullName) + : GetType (fullName); + } + public TypeDefinition GetType (string fullName) { CheckFullName (fullName); @@ -493,6 +532,26 @@ namespace Mono.Cecil { return ((TypeDefinitionCollection) this.Types).GetType (@namespace ?? string.Empty, name); } + public IEnumerable<TypeDefinition> GetTypes () + { + return GetTypes (Types); + } + + static IEnumerable<TypeDefinition> GetTypes (Collection<TypeDefinition> types) + { + for (int i = 0; i < types.Count; i++) { + var type = types [i]; + + yield return type; + + if (!type.HasNestedTypes) + continue; + + foreach (var nested in GetTypes (type.NestedTypes)) + yield return nested; + } + } + static void CheckFullName (string fullName) { if (fullName == null) @@ -522,17 +581,17 @@ namespace Mono.Cecil { internal FieldDefinition Resolve (FieldReference field) { - return MetadataResolver.Resolve (AssemblyResolver, field); + return MetadataResolver.Resolve (field); } internal MethodDefinition Resolve (MethodReference method) { - return MetadataResolver.Resolve (AssemblyResolver, method); + return MetadataResolver.Resolve (method); } internal TypeDefinition Resolve (TypeReference type) { - return MetadataResolver.Resolve (AssemblyResolver, type); + return MetadataResolver.Resolve (type); } #if !READ_ONLY @@ -564,89 +623,57 @@ namespace Mono.Cecil { throw new ArgumentException (); } -#if !CF - public TypeReference Import (Type type) + static ImportGenericContext GenericContextFor (IGenericParameterProvider context) { - CheckType (type); - - return MetadataImporter.ImportType (type, null, ImportGenericKind.Definition); + return context != null ? new ImportGenericContext (context) : default (ImportGenericContext); } - public TypeReference Import (Type type, TypeReference context) - { - return Import (type, (IGenericParameterProvider) context); - } +#if !CF - public TypeReference Import (Type type, MethodReference context) + public TypeReference Import (Type type) { - return Import (type, (IGenericParameterProvider) context); + return Import (type, null); } - TypeReference Import (Type type, IGenericParameterProvider context) + public TypeReference Import (Type type, IGenericParameterProvider context) { CheckType (type); CheckContext (context, this); return MetadataImporter.ImportType ( type, - (IGenericContext) context, - context != null - ? ImportGenericKind.Open - : ImportGenericKind.Definition); + GenericContextFor (context), + context != null ? ImportGenericKind.Open : ImportGenericKind.Definition); } public FieldReference Import (SR.FieldInfo field) { - CheckField (field); - - return MetadataImporter.ImportField (field, null); - } - - public FieldReference Import (SR.FieldInfo field, TypeReference context) - { - return Import (field, (IGenericParameterProvider) context); + return Import (field, null); } - public FieldReference Import (SR.FieldInfo field, MethodReference context) - { - return Import (field, (IGenericParameterProvider) context); - } - - FieldReference Import (SR.FieldInfo field, IGenericParameterProvider context) + public FieldReference Import (SR.FieldInfo field, IGenericParameterProvider context) { CheckField (field); CheckContext (context, this); - return MetadataImporter.ImportField (field, (IGenericContext) context); + return MetadataImporter.ImportField (field, GenericContextFor (context)); } public MethodReference Import (SR.MethodBase method) { CheckMethod (method); - return MetadataImporter.ImportMethod (method, null, ImportGenericKind.Definition); - } - - public MethodReference Import (SR.MethodBase method, TypeReference context) - { - return Import (method, (IGenericParameterProvider) context); - } - - public MethodReference Import (SR.MethodBase method, MethodReference context) - { - return Import (method, (IGenericParameterProvider) context); + return MetadataImporter.ImportMethod (method, default (ImportGenericContext), ImportGenericKind.Definition); } - MethodReference Import (SR.MethodBase method, IGenericParameterProvider context) + public MethodReference Import (SR.MethodBase method, IGenericParameterProvider context) { CheckMethod (method); CheckContext (context, this); return MetadataImporter.ImportMethod (method, - (IGenericContext) context, - context != null - ? ImportGenericKind.Open - : ImportGenericKind.Definition); + GenericContextFor (context), + context != null ? ImportGenericKind.Open : ImportGenericKind.Definition); } #endif @@ -657,20 +684,10 @@ namespace Mono.Cecil { if (type.Module == this) return type; - return MetadataImporter.ImportType (type, null); - } - - public TypeReference Import (TypeReference type, TypeReference context) - { - return Import (type, (IGenericParameterProvider) context); - } - - public TypeReference Import (TypeReference type, MethodReference context) - { - return Import (type, (IGenericParameterProvider) context); + return MetadataImporter.ImportType (type, default (ImportGenericContext)); } - TypeReference Import (TypeReference type, IGenericParameterProvider context) + public TypeReference Import (TypeReference type, IGenericParameterProvider context) { CheckType (type); @@ -679,7 +696,7 @@ namespace Mono.Cecil { CheckContext (context, this); - return MetadataImporter.ImportType (type, (IGenericContext) context); + return MetadataImporter.ImportType (type, GenericContextFor (context)); } public FieldReference Import (FieldReference field) @@ -689,20 +706,10 @@ namespace Mono.Cecil { if (field.Module == this) return field; - return MetadataImporter.ImportField (field, null); - } - - public FieldReference Import (FieldReference field, TypeReference context) - { - return Import (field, (IGenericParameterProvider) context); + return MetadataImporter.ImportField (field, default (ImportGenericContext)); } - public FieldReference Import (FieldReference field, MethodReference context) - { - return Import (field, (IGenericParameterProvider) context); - } - - FieldReference Import (FieldReference field, IGenericParameterProvider context) + public FieldReference Import (FieldReference field, IGenericParameterProvider context) { CheckField (field); @@ -711,30 +718,15 @@ namespace Mono.Cecil { CheckContext (context, this); - return MetadataImporter.ImportField (field, (IGenericContext) context); + return MetadataImporter.ImportField (field, GenericContextFor (context)); } public MethodReference Import (MethodReference method) { - CheckMethod (method); - - if (method.Module == this) - return method; - - return MetadataImporter.ImportMethod (method, null); - } - - public MethodReference Import (MethodReference method, TypeReference context) - { - return Import (method, (IGenericParameterProvider) context); + return Import (method, null); } - public MethodReference Import (MethodReference method, MethodReference context) - { - return Import (method, (IGenericParameterProvider) context); - } - - MethodReference Import (MethodReference method, IGenericParameterProvider context) + public MethodReference Import (MethodReference method, IGenericParameterProvider context) { CheckMethod (method); @@ -743,7 +735,7 @@ namespace Mono.Cecil { CheckContext (context, this); - return MetadataImporter.ImportMethod (method, (IGenericContext) context); + return MetadataImporter.ImportMethod (method, GenericContextFor (context)); } #endif @@ -755,7 +747,7 @@ namespace Mono.Cecil { public IMetadataTokenProvider LookupToken (MetadataToken token) { - return Read (this, (_, reader) => reader.LookupToken (token)); + return Read (token, (t, reader) => reader.LookupToken (t)); } internal TRet Read<TItem, TRet> (TItem item, Func<TItem, MetadataReader, TRet> read) @@ -771,15 +763,27 @@ namespace Mono.Cecil { return ret; } + public bool HasDebugHeader { + get { return Image != null && !Image.Debug.IsZero; } + } + + public ImageDebugDirectory GetDebugHeader (out byte [] header) + { + if (!HasDebugHeader) + throw new InvalidOperationException (); + + return Image.GetDebugHeader (out header); + } + void ProcessDebugHeader () { - if (Image == null || Image.Debug.IsZero) + if (!HasDebugHeader) return; byte [] header; - var directory = Image.GetDebugHeader (out header); + var directory = GetDebugHeader (out header); - if (!SymbolReader.ProcessDebugHeader (directory, header)) + if (!symbol_reader.ProcessDebugHeader (directory, header)) throw new InvalidOperationException (); } @@ -802,15 +806,19 @@ namespace Mono.Cecil { architecture = parameters.Architecture, mvid = Guid.NewGuid (), Attributes = ModuleAttributes.ILOnly, + Characteristics = (ModuleCharacteristics) 0x8540, }; if (parameters.AssemblyResolver != null) module.assembly_resolver = parameters.AssemblyResolver; + if (parameters.MetadataResolver != null) + module.metadata_resolver = parameters.MetadataResolver; + if (parameters.Kind != ModuleKind.NetModule) { var assembly = new AssemblyDefinition (); module.assembly = assembly; - module.assembly.Name = new AssemblyNameDefinition (name, new Version (0, 0)); + module.assembly.Name = CreateAssemblyName (name); assembly.main_module = module; } @@ -819,6 +827,14 @@ namespace Mono.Cecil { return module; } + static AssemblyNameDefinition CreateAssemblyName (string name) + { + if (name.EndsWith (".dll") || name.EndsWith (".exe")) + name = name.Substring (0, name.Length - 4); + + return new AssemblyNameDefinition (name, new Version (0, 0, 0, 0)); + } + #endif public void ReadSymbols () @@ -827,10 +843,10 @@ namespace Mono.Cecil { throw new InvalidOperationException (); var provider = SymbolProvider.GetPlatformReaderProvider (); + if (provider == null) + throw new InvalidOperationException (); - SymbolReader = provider.GetSymbolReader (this, fq_name); - - ProcessDebugHeader (); + ReadSymbols (provider.GetSymbolReader (this, fq_name)); } public void ReadSymbols (ISymbolReader reader) @@ -838,7 +854,7 @@ namespace Mono.Cecil { if (reader == null) throw new ArgumentNullException ("reader"); - SymbolReader = reader; + symbol_reader = reader; ProcessDebugHeader (); } @@ -934,6 +950,14 @@ namespace Mono.Cecil { return self != null && self.HasImage; } + public static bool IsCorlib (this ModuleDefinition module) + { + if (module.Assembly == null) + return false; + + return module.Assembly.Name.Name == "mscorlib"; + } + public static string GetFullyQualifiedName (this Stream self) { #if !SILVERLIGHT diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleKind.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleKind.cs index eb57890..c29da88 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleKind.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleKind.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -41,6 +41,7 @@ namespace Mono.Cecil { I386, AMD64, IA64, + ARMv7, } [Flags] @@ -48,5 +49,16 @@ namespace Mono.Cecil { ILOnly = 1, Required32Bit = 2, StrongNameSigned = 8, + Preferred32Bit = 0x00020000, + } + + [Flags] + public enum ModuleCharacteristics { + HighEntropyVA = 0x0020, + DynamicBase = 0x0040, + NoSEH = 0x0400, + NXCompat = 0x0100, + AppContainer = 0x1000, + TerminalServerAware = 0x8000, } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs index 0b6e49a..3934b3c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/NativeType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/NativeType.cs index afa107f..88da980 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/NativeType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/NativeType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeAttributes.cs index fc94692..bb36838 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -53,7 +53,7 @@ namespace Mono.Cecil { BestFitMask = 0x0030, BestFitEnabled = 0x0010, - BestFidDisabled = 0x0020, + BestFitDisabled = 0x0020, ThrowOnUnmappableCharMask = 0x3000, ThrowOnUnmappableCharEnabled = 0x1000, diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeInfo.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeInfo.cs index 53aae2c..cfd817c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeInfo.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PInvokeInfo.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -106,14 +106,14 @@ namespace Mono.Cecil { set { attributes = attributes.SetMaskedAttributes ((ushort) PInvokeAttributes.CallConvMask, (ushort) PInvokeAttributes.CallConvFastcall, value); } } - public bool IsBestFistEnabled { + public bool IsBestFitEnabled { get { return attributes.GetMaskedAttributes ((ushort) PInvokeAttributes.BestFitMask, (ushort) PInvokeAttributes.BestFitEnabled); } set { attributes = attributes.SetMaskedAttributes ((ushort) PInvokeAttributes.BestFitMask, (ushort) PInvokeAttributes.BestFitEnabled, value); } } - public bool IsBestFistDisabled { - get { return attributes.GetMaskedAttributes ((ushort) PInvokeAttributes.BestFitMask, (ushort) PInvokeAttributes.BestFidDisabled); } - set { attributes = attributes.SetMaskedAttributes ((ushort) PInvokeAttributes.BestFitMask, (ushort) PInvokeAttributes.BestFidDisabled, value); } + public bool IsBestFitDisabled { + get { return attributes.GetMaskedAttributes ((ushort) PInvokeAttributes.BestFitMask, (ushort) PInvokeAttributes.BestFitDisabled); } + set { attributes = attributes.SetMaskedAttributes ((ushort) PInvokeAttributes.BestFitMask, (ushort) PInvokeAttributes.BestFitDisabled, value); } } public bool IsThrowOnUnmappableCharEnabled { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs index 8c12b54..e0bc825 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs index 059f257..26152bd 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -49,6 +49,15 @@ namespace Mono.Cecil { get { return method; } } + public int Sequence { + get { + if (method == null) + return -1; + + return method.HasImplicitThis () ? index + 1 : index; + } + } + public bool HasConstant { get { ResolveConstant (); @@ -137,6 +146,12 @@ namespace Mono.Cecil { #endregion + internal ParameterDefinition (TypeReference parameterType, IMethodSignature method) + : this (string.Empty, ParameterAttributes.None, parameterType) + { + this.method = method; + } + public ParameterDefinition (TypeReference parameterType) : this (string.Empty, ParameterAttributes.None, parameterType) { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinitionCollection.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinitionCollection.cs index 86538bb..bd8b1c1 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinitionCollection.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinitionCollection.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterReference.cs index cfe648f..46b057c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PinnedType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PinnedType.cs index d5e7a80..ff59cfb 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PinnedType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PinnedType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PointerType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PointerType.cs index 54ab775..a142e14 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PointerType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PointerType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyAttributes.cs index ad6fd9e..1be0413 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyDefinition.cs index 835a929..42878a0 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -127,6 +127,8 @@ namespace Mono.Cecil { public bool HasParameters { get { + InitializeMethods (); + if (get_method != null) return get_method.HasParameters; @@ -254,5 +256,10 @@ namespace Mono.Cecil { module.Read (this, (property, reader) => reader.ReadMethods (property)); } + + public override PropertyDefinition Resolve () + { + return this; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyReference.cs index 5022cd3..0dcfc95 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -53,5 +53,7 @@ namespace Mono.Cecil { property_type = propertyType; } + + public abstract PropertyDefinition Resolve (); } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ReferenceType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ReferenceType.cs index 3a6b326..7940c61 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ReferenceType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ReferenceType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Resource.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Resource.cs index 0432ae7..eff7f41 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Resource.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Resource.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs index 4e57ada..837472a 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -93,6 +93,7 @@ namespace Mono.Cecil { public sealed class SecurityDeclaration { readonly internal uint signature; + byte [] blob; readonly ModuleDefinition module; internal bool resolved; @@ -137,12 +138,22 @@ namespace Mono.Cecil { this.resolved = true; } + public SecurityDeclaration (SecurityAction action, byte [] blob) + { + this.action = action; + this.resolved = false; + this.blob = blob; + } + public byte [] GetBlob () { + if (blob != null) + return blob; + if (!HasImage || signature == 0) throw new NotSupportedException (); - return module.Read (this, (declaration, reader) => reader.ReadSecurityDeclarationBlob (declaration.signature)); ; + return blob = module.Read (this, (declaration, reader) => reader.ReadSecurityDeclarationBlob (declaration.signature)); } void Resolve () @@ -165,9 +176,7 @@ namespace Mono.Cecil { this ISecurityDeclarationProvider self, ModuleDefinition module) { - return module.HasImage () - ? module.Read (self, (provider, reader) => reader.HasSecurityDeclarations (provider)) - : false; + return module.HasImage () && module.Read (self, (provider, reader) => reader.HasSecurityDeclarations (provider)); } public static Collection<SecurityDeclaration> GetSecurityDeclarations ( diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SentinelType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SentinelType.cs index d0d5a1d..664d75b 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SentinelType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SentinelType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TargetRuntime.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TargetRuntime.cs index 9931fc6..9b49a5f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TargetRuntime.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TargetRuntime.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeAttributes.cs index 6e263e8..86fbc4d 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeAttributes.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -62,6 +62,7 @@ namespace Mono.Cecil { // Implementation attributes Import = 0x00001000, // Class/Interface is imported Serializable = 0x00002000, // Class is serializable + WindowsRuntime = 0x00004000, // Windows Runtime type // String formatting attributes StringFormatMask = 0x00030000, // Use this mask to retrieve string information for native interop diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinition.cs index c83531a..58c52af 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinition.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -28,6 +28,7 @@ using System; +using Mono.Cecil.Metadata; using Mono.Collections.Generic; namespace Mono.Cecil { @@ -161,10 +162,6 @@ namespace Mono.Cecil { } } - internal new bool HasImage { - get { return Module != null && Module.HasImage; } - } - public bool HasMethods { get { if (methods != null) @@ -392,6 +389,11 @@ namespace Mono.Cecil { set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Serializable, value); } } + public bool IsWindowsRuntime { + get { return attributes.GetAttributes ((uint) TypeAttributes.WindowsRuntime); } + set { attributes = attributes.SetAttributes ((uint) TypeAttributes.WindowsRuntime, value); } + } + public bool IsAnsiClass { get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass); } set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass, value); } @@ -437,6 +439,23 @@ namespace Mono.Cecil { } } + public override bool IsPrimitive { + get { + ElementType primitive_etype; + return MetadataSystem.TryGetPrimitiveElementType (this, out primitive_etype); + } + } + + public override MetadataType MetadataType { + get { + ElementType primitive_etype; + if (MetadataSystem.TryGetPrimitiveElementType (this, out primitive_etype)) + return (MetadataType) primitive_etype; + + return base.MetadataType; + } + } + public override bool IsDefinition { get { return true; } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinitionCollection.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinitionCollection.cs index 702fe27..eae7122 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinitionCollection.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinitionCollection.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs index df6acf3..90e04a6 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -173,13 +173,7 @@ namespace Mono.Cecil { return; } -#if !CF - Array.Resize (ref array, array.Length + 1); -#else - var copy = new T [array.Length + 1]; - Array.Copy (array, copy, array.Length); - array = copy; -#endif + array = array.Resize (array.Length + 1); array [array.Length - 1] = item; } @@ -272,7 +266,7 @@ namespace Mono.Cecil { public static TypeReference ParseType (ModuleDefinition module, string fullname) { - if (fullname == null) + if (string.IsNullOrEmpty (fullname)) return null; var parser = new TypeParser (fullname); @@ -356,6 +350,7 @@ namespace Mono.Cecil { SplitFullName (type_info.type_fullname, out @namespace, out name); var type = new TypeReference (@namespace, name, module, scope); + MetadataSystem.TryProcessPrimitiveTypeReference (type); AdjustGenericParameters (type); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeReference.cs index e45d823..f811b4f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeReference.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -135,7 +135,7 @@ namespace Mono.Cecil { if (generic_parameters != null) return generic_parameters; - return generic_parameters = new Collection<GenericParameter> (); + return generic_parameters = new GenericParameterCollection (this); } } @@ -147,6 +147,15 @@ namespace Mono.Cecil { return scope; } + set { + var declaring_type = this.DeclaringType; + if (declaring_type != null) { + declaring_type.Scope = value; + return; + } + + scope = value; + } } public bool IsNested { @@ -216,28 +225,8 @@ namespace Mono.Cecil { get { return false; } } - public bool IsPrimitive { - get { - switch (etype) { - case ElementType.Boolean: - case ElementType.Char: - case ElementType.I: - case ElementType.U: - 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: - return true; - default: - return false; - } - } + public virtual bool IsPrimitive { + get { return etype.IsPrimitive (); } } public virtual MetadataType MetadataType { @@ -288,6 +277,29 @@ namespace Mono.Cecil { static partial class Mixin { + public static bool IsPrimitive (this ElementType self) + { + switch (self) { + case ElementType.Boolean: + case ElementType.Char: + case ElementType.I: + case ElementType.U: + 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: + return true; + default: + return false; + } + } + public static bool IsTypeOf (this TypeReference self, string @namespace, string name) { return self.Name == name @@ -319,7 +331,7 @@ namespace Mono.Cecil { { var type = self.Resolve (); if (type == null) - throw new InvalidOperationException (string.Format ("Failed to resolve type: {0}", self.FullName)); + throw new ResolutionException (self); return type; } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs index da4e726..75651be 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -42,16 +42,17 @@ namespace Mono.Cecil { public override string Name { get { return element_type.Name; } - set { throw new NotSupportedException (); } + set { throw new InvalidOperationException (); } } public override string Namespace { get { return element_type.Namespace; } - set { throw new NotSupportedException (); } + set { throw new InvalidOperationException (); } } public override IMetadataScope Scope { get { return element_type.Scope; } + set { throw new InvalidOperationException (); } } public override ModuleDefinition Module { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs index 0b0ba91..63f6aea 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -34,20 +34,29 @@ namespace Mono.Cecil { public abstract class TypeSystem { - sealed class CorlibTypeSystem : TypeSystem { + sealed class CoreTypeSystem : TypeSystem { - public CorlibTypeSystem (ModuleDefinition module) + public CoreTypeSystem (ModuleDefinition module) : base (module) { } internal override TypeReference LookupType (string @namespace, string name) { + var type = LookupTypeDefinition (@namespace, name) ?? LookupTypeForwarded (@namespace, name); + if (type != null) + return type; + + throw new NotSupportedException (); + } + + TypeReference LookupTypeDefinition (string @namespace, string name) + { var metadata = module.MetadataSystem; if (metadata.Types == null) Initialize (module.Types); - return module.Read (this, (_, reader) => { + return module.Read (new Row<string, string> (@namespace, name), (row, reader) => { var types = reader.metadata.Types; for (int i = 0; i < types.Length; i++) { @@ -56,7 +65,7 @@ namespace Mono.Cecil { var type = types [i]; - if (type.Name == name && type.Namespace == @namespace) + if (type.Name == row.Col2 && type.Namespace == row.Col1) return type; } @@ -64,6 +73,22 @@ namespace Mono.Cecil { }); } + TypeReference LookupTypeForwarded (string @namespace, string name) + { + if (!module.HasExportedTypes) + return null; + + var exported_types = module.ExportedTypes; + for (int i = 0; i < exported_types.Count; i++) { + var exported_type = exported_types [i]; + + if (exported_type.Name == name && exported_type.Namespace == @namespace) + return exported_type.CreateReference (); + } + + return null; + } + static void Initialize (object obj) { } @@ -158,20 +183,12 @@ namespace Mono.Cecil { internal static TypeSystem CreateTypeSystem (ModuleDefinition module) { - if (IsCorlib (module)) - return new CorlibTypeSystem (module); + if (module.IsCorlib ()) + return new CoreTypeSystem (module); return new CommonTypeSystem (module); } - static bool IsCorlib (ModuleDefinition module) - { - if (module.Assembly == null) - return false; - - return module.Assembly.Name.Name == "mscorlib"; - } - internal abstract TypeReference LookupType (string @namespace, string name); TypeReference LookupSystemType (string name, ElementType element_type) diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/VariantType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/VariantType.cs index 86d6daa..76562f6 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/VariantType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/VariantType.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/Collection.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/Collection.cs index 1137978..4d92717 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/Collection.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/Collection.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -115,6 +115,9 @@ namespace Mono.Collections.Generic { public Collection (ICollection<T> items) { + if (items == null) + throw new ArgumentNullException ("items"); + this.items = new T [items.Count]; items.CopyTo (this.items, 0); this.size = this.items.Length; diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/ReadOnlyCollection.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/ReadOnlyCollection.cs index 35c9cd8..7f24df6 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/ReadOnlyCollection.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/ReadOnlyCollection.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -28,10 +28,11 @@ using System; using System.Collections; +using System .Collections.Generic; namespace Mono.Collections.Generic { - public sealed class ReadOnlyCollection<T> : Collection<T>, IList { + public sealed class ReadOnlyCollection<T> : Collection<T>, ICollection<T>, IList { static ReadOnlyCollection<T> empty; @@ -39,6 +40,14 @@ namespace Mono.Collections.Generic { get { return empty ?? (empty = new ReadOnlyCollection<T> ()); } } + bool ICollection<T>.IsReadOnly { + get { return true; } + } + + bool IList.IsFixedSize { + get { return true; } + } + bool IList.IsReadOnly { get { return true; } } @@ -52,8 +61,7 @@ namespace Mono.Collections.Generic { if (array == null) throw new ArgumentNullException (); - this.items = array; - this.size = array.Length; + Initialize (array, array.Length); } public ReadOnlyCollection (Collection<T> collection) @@ -61,8 +69,14 @@ namespace Mono.Collections.Generic { if (collection == null) throw new ArgumentNullException (); - this.items = collection.items; - this.size = collection.size; + Initialize (collection.items, collection.size); + } + + void Initialize (T [] items, int size) + { + this.items = new T [size]; + Array.Copy (items, 0, this.items, 0, size); + this.size = size; } internal override void Grow (int desired) diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Security.Cryptography/CryptoService.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Security.Cryptography/CryptoService.cs index f504f5e..58b6cfa 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Security.Cryptography/CryptoService.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Security.Cryptography/CryptoService.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Actions.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Actions.cs index 071b149..756438f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Actions.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Actions.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -30,7 +30,7 @@ namespace Mono { //delegate void Action (); - //delegate void Action<T1, T2> (T1 arg1, T2 arg2); + delegate void Action<T1, T2> (T1 arg1, T2 arg2); //delegate void Action<T1, T2, T3> (T1 arg1, T2 arg2, T3 arg3); //delegate void Action<T1, T2, T3, T4> (T1 arg1, T2 arg2, T3 arg3, T4 arg4); } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Empty.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Empty.cs index 70739af..c9e5d7d 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Empty.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Empty.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -27,8 +27,7 @@ // using System; -using System.Collections; -using System.Collections.Generic; +using Mono.Collections.Generic; namespace Mono { @@ -47,9 +46,22 @@ namespace Mono.Cecil { return self == null || self.Length == 0; } - public static bool IsNullOrEmpty<T> (this ICollection<T> self) + public static bool IsNullOrEmpty<T> (this Collection<T> self) { - return self == null || self.Count == 0; + return self == null || self.size == 0; + } + + public static T [] Resize<T> (this T [] self, int length) + { +#if !CF + Array.Resize (ref self, length); +#else + var copy = new T [length]; + Array.Copy (self, copy, self.Length); + self = copy; +#endif + + return self; } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Funcs.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Funcs.cs index 40f2225..87bc6e9 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Funcs.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono/Funcs.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/System.Runtime.CompilerServices/ExtensionAttribute.cs b/Mono.Addins.CecilReflector/Mono.Cecil/System.Runtime.CompilerServices/ExtensionAttribute.cs index ae79cda..bb24f98 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/System.Runtime.CompilerServices/ExtensionAttribute.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/System.Runtime.CompilerServices/ExtensionAttribute.cs @@ -4,7 +4,7 @@ // Author: // Jb Evain (jbevain@gmail.com) // -// Copyright (c) 2008 - 2010 Jb Evain +// Copyright (c) 2008 - 2011 Jb Evain // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the |