diff options
author | Lluis Sanchez Gual <lluis@novell.com> | 2011-02-24 01:29:16 +0300 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@novell.com> | 2011-02-24 02:46:44 +0300 |
commit | 5b0bccf90ff5cc2ce9bdbb5d043a024f55abb259 (patch) | |
tree | 3f02f98f58451ee6226a9bfeabdd881108579426 /Mono.Addins.CecilReflector | |
parent | f36a2abc6fccfcb00188f1a87289a7f2e6683128 (diff) |
Updated.
Diffstat (limited to 'Mono.Addins.CecilReflector')
25 files changed, 295 insertions, 134 deletions
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 c780cc7..4cfc7db 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeReader.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Cil/CodeReader.cs @@ -123,7 +123,7 @@ namespace Mono.Cecil.Cil { body.local_var_token = new MetadataToken (ReadUInt32 ()); body.init_locals = (flags & 0x10) != 0; - if (body.LocalVarToken.RID != 0) + if (body.local_var_token.RID != 0) body.variables = ReadVariables (body.local_var_token); ReadCode (); @@ -145,6 +145,10 @@ namespace Mono.Cecil.Cil { { start = position; var code_size = body.code_size; + + if (code_size < 0 || buffer.Length <= (uint) (code_size + position)) + code_size = 0; + var end = start + code_size; var instructions = body.instructions = new InstructionCollection (code_size / 3); @@ -231,7 +235,7 @@ namespace Mono.Cecil.Cil { public VariableDefinition GetVariable (int index) { - return body.Variables [index]; + return body.GetVariable (index); } public CallSite GetCallSite (MetadataToken token) 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 f3c482b..1ede042 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/ElementType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.Metadata/ElementType.cs @@ -28,40 +28,6 @@ namespace Mono.Cecil.Metadata { - public enum MetadataType : byte { - Void = 0x01, - Boolean = 0x02, - Char = 0x03, - SByte = 0x04, - Byte = 0x05, - Int16 = 0x06, - UInt16 = 0x07, - Int32 = 0x08, - UInt32 = 0x09, - Int64 = 0x0a, - UInt64 = 0x0b, - Single = 0x0c, - Double = 0x0d, - String = 0x0e, - Pointer = 0x0f, - ByReference = 0x10, - ValueType = 0x11, - Class = 0x12, - Var = 0x13, - Array = 0x14, - GenericInstance = 0x15, - TypedByReference = 0x16, - IntPtr = 0x18, - UIntPtr = 0x19, - FunctionPointer = 0x1b, - Object = 0x1c, - MVar = 0x1e, - RequiredModifier = 0x1f, - OptionalModifier = 0x20, - Sentinel = 0x41, - Pinned = 0x45, - } - enum ElementType : byte { None = 0x00, Void = 0x01, 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 fd78025..fe56a6f 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBufferEqualityComparer.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil.PE/ByteBufferEqualityComparer.cs @@ -50,12 +50,29 @@ namespace Mono.Cecil.PE { public int GetHashCode (ByteBuffer buffer) { +#if !BYTE_BUFFER_WELL_DISTRIBUTED_HASH var hash = 0; var bytes = buffer.buffer; for (int i = 0; i < buffer.length; i++) hash = (hash * 37) ^ bytes [i]; return hash; +#else + const uint p = 16777619; + uint hash = 2166136261; + + var bytes = buffer.buffer; + for (int i = 0; i < buffer.length; i++) + hash = (hash ^ bytes [i]) * p; + + hash += hash << 13; + hash ^= hash >> 7; + hash += hash << 3; + hash ^= hash >> 17; + hash += hash << 5; + + return (int) hash; +#endif } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs index edbc957..2cafb99 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyReader.cs @@ -83,7 +83,7 @@ namespace Mono.Cecil { ReadSymbols (module, parameters); if (parameters.AssemblyResolver != null) - module.AssemblyResolver = parameters.AssemblyResolver; + module.assembly_resolver = parameters.AssemblyResolver; return module; } @@ -900,7 +900,7 @@ namespace Mono.Cecil { return (TypeReference) LookupToken (token); } - TypeDefinition GetTypeDefinition (uint rid) + public TypeDefinition GetTypeDefinition (uint rid) { InitializeTypeDefinitions (); @@ -957,11 +957,7 @@ namespace Mono.Cecil { if (type != null) return type; - type = ReadTypeReference (rid); - if (type != null) - metadata.AddTypeReference (type); - - return type; + return ReadTypeReference (rid); } TypeReference ReadTypeReference (uint rid) @@ -974,6 +970,19 @@ namespace Mono.Cecil { var scope_token = ReadMetadataToken (CodedIndex.ResolutionScope); + var name = ReadString (); + var @namespace = ReadString (); + + var type = new TypeReference ( + @namespace, + name, + module, + null); + + type.token = new MetadataToken (TokenType.TypeRef, rid); + + metadata.AddTypeReference (type); + if (scope_token.TokenType == TokenType.TypeRef) { declaring_type = GetTypeDefOrRef (scope_token); @@ -983,17 +992,8 @@ namespace Mono.Cecil { } else scope = GetTypeReferenceScope (scope_token); - var name = ReadString (); - var @namespace = ReadString (); - - var type = new TypeReference ( - @namespace, - name, - scope); - + type.scope = scope; type.DeclaringType = declaring_type; - type.token = new MetadataToken (TokenType.TypeRef, rid); - type.module = module; MetadataSystem.TryProcessPrimitiveType (type); @@ -1037,7 +1037,11 @@ namespace Mono.Cecil { return null; var reader = ReadSignature (ReadBlobIndex ()); - return reader.ReadTypeSignature (); + var type = reader.ReadTypeSignature (); + if (type.token.RID == 0) + type.token = new MetadataToken (TokenType.TypeSpec, rid); + + return type; } SignatureReader ReadSignature (uint signature) @@ -1413,14 +1417,13 @@ namespace Mono.Cecil { return ReadListRange (rid, Table.PropertyMap, Table.Property); } - public MethodSemanticsAttributes ReadMethodSemantics (MethodDefinition method) + MethodSemanticsAttributes ReadMethodSemantics (MethodDefinition method) { InitializeMethodSemantics (); Row<MethodSemanticsAttributes, MetadataToken> row; if (!metadata.Semantics.TryGetValue (method.token.RID, out row)) return MethodSemanticsAttributes.None; - method.SemanticsAttributes = row.Col1; var type = method.DeclaringType; switch (row.Col1) { @@ -1528,11 +1531,23 @@ namespace Mono.Cecil { return @event; } - static void ReadAllSemantics (TypeDefinition type) + public MethodSemanticsAttributes ReadAllSemantics (MethodDefinition method) + { + ReadAllSemantics (method.DeclaringType); + + return method.SemanticsAttributes; + } + + void ReadAllSemantics (TypeDefinition type) { var methods = type.Methods; - for (int i = 0; i < methods.Count; i++) - methods [i].ReadSemantics (); + for (int i = 0; i < methods.Count; i++) { + var method = methods [i]; + if (method.sem_attrs.HasValue) + continue; + + method.sem_attrs = ReadMethodSemantics (method); + } } Range ReadParametersRange (uint method_rid) @@ -1929,7 +1944,7 @@ namespace Mono.Cecil { element = GetMethodSpecification (rid); break; default: - throw new NotSupportedException (); + return null; } this.position = position; @@ -1953,7 +1968,7 @@ namespace Mono.Cecil { { var type = metadata.GetFieldDeclaringType (rid); if (type == null) - throw new NotSupportedException (); + return null; InitializeCollection (type.Fields); @@ -1975,7 +1990,7 @@ namespace Mono.Cecil { { var type = metadata.GetMethodDeclaringType (rid); if (type == null) - throw new NotSupportedException (); + return null; InitializeCollection (type.Methods); @@ -1987,11 +2002,13 @@ namespace Mono.Cecil { if (!MoveTo (Table.MethodSpec, rid)) return null; - var method = (MethodReference) LookupToken ( + var element_method = (MethodReference) LookupToken ( ReadMetadataToken (CodedIndex.MethodDefOrRef)); var signature = ReadBlobIndex (); - return ReadMethodSpecSignature (signature, method); + var method_spec = ReadMethodSpecSignature (signature, element_method); + method_spec.token = new MetadataToken (TokenType.MethodSpec, rid); + return method_spec; } MethodSpecification ReadMethodSpecSignature (uint signature, MethodReference method) @@ -2020,7 +2037,7 @@ namespace Mono.Cecil { return member; member = ReadMemberReference (rid); - if (!member.ContainsGenericParameter) + if (member != null && !member.ContainsGenericParameter) metadata.AddMemberReference (member); return member; } @@ -2114,7 +2131,7 @@ namespace Mono.Cecil { var type_system = module.TypeSystem; var context = new MethodReference (string.Empty, type_system.Void); - context.DeclaringType = new TypeReference (string.Empty, string.Empty, type_system.Corlib); + context.DeclaringType = new TypeReference (string.Empty, string.Empty, module, type_system.Corlib); var member_references = new MemberReference [length]; diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs index a60004b..7aad247 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs @@ -1413,8 +1413,7 @@ namespace Mono.Cecil { GetBlobIndex (GetMethodSignature (method)), param_rid)); - if (method.HasParameters) - AddParameters (method); + AddParameters (method); if (method.HasGenericParameters) AddGenericParameters (method); @@ -1434,13 +1433,16 @@ namespace Mono.Cecil { void AddParameters (MethodDefinition method) { - var parameters = method.Parameters; - var return_parameter = method.MethodReturnType.parameter; if (return_parameter != null && RequiresParameterRow (return_parameter)) AddParameter (0, return_parameter, param_table); + if (!method.HasParameters) + return; + + var parameters = method.Parameters; + for (int i = 0; i < parameters.Count; i++) { var parameter = parameters [i]; if (!RequiresParameterRow (parameter)) @@ -1958,11 +1960,20 @@ namespace Mono.Cecil { return signature; } + static Exception CreateForeignMemberException (MemberReference member) + { + return new ArgumentException (string.Format ("Member '{0}' is declared in another module and needs to be imported", member)); + } + public MetadataToken LookupToken (IMetadataTokenProvider provider) { if (provider == null) throw new ArgumentNullException (); + var member = provider as MemberReference; + if (member == null || member.Module != module) + throw CreateForeignMemberException (member); + var token = provider.MetadataToken; switch (token.TokenType) { @@ -1979,7 +1990,7 @@ namespace Mono.Cecil { case TokenType.MethodSpec: return GetMethodSpecToken ((MethodSpecification) provider); case TokenType.MemberRef: - return GetMemberRefToken ((MemberReference) provider); + return GetMemberRefToken (member); default: throw new NotSupportedException (); } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs index 0b8f08f..e449a03 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/BaseAssemblyResolver.cs @@ -80,7 +80,15 @@ namespace Mono.Cecil { public virtual AssemblyDefinition Resolve (string fullName) { - return Resolve (AssemblyNameReference.Parse (fullName)); + return Resolve (fullName, new ReaderParameters ()); + } + + public virtual AssemblyDefinition Resolve (string fullName, ReaderParameters parameters) + { + if (fullName == null) + throw new ArgumentNullException ("fullName"); + + return Resolve (AssemblyNameReference.Parse (fullName), parameters); } public event AssemblyResolveEventHandler ResolveFailure; @@ -90,14 +98,27 @@ namespace Mono.Cecil { directories = new Collection<string> (2) { ".", "bin" }; } - AssemblyDefinition GetAssembly (string file) + AssemblyDefinition GetAssembly (string file, ReaderParameters parameters) { - return ModuleDefinition.ReadModule (file, new ReaderParameters { AssemblyResolver = this}).Assembly; + if (parameters.AssemblyResolver == null) + parameters.AssemblyResolver = this; + + return ModuleDefinition.ReadModule (file, parameters).Assembly; } public virtual AssemblyDefinition Resolve (AssemblyNameReference name) { - var assembly = SearchDirectory (name, directories); + return Resolve (name, new ReaderParameters ()); + } + + public virtual AssemblyDefinition Resolve (AssemblyNameReference name, ReaderParameters parameters) + { + if (name == null) + throw new ArgumentNullException ("name"); + if (parameters == null) + parameters = new ReaderParameters (); + + var assembly = SearchDirectory (name, directories, parameters); if (assembly != null) return assembly; @@ -105,22 +126,22 @@ namespace Mono.Cecil { var framework_dir = Path.GetDirectoryName (typeof (object).Module.FullyQualifiedName); if (IsZero (name.Version)) { - assembly = SearchDirectory (name, new [] { framework_dir }); + assembly = SearchDirectory (name, new [] { framework_dir }, parameters); if (assembly != null) return assembly; } if (name.Name == "mscorlib") { - assembly = GetCorlib (name); + assembly = GetCorlib (name, parameters); if (assembly != null) return assembly; } - assembly = GetAssemblyInGac (name); + assembly = GetAssemblyInGac (name, parameters); if (assembly != null) return assembly; - assembly = SearchDirectory (name, new [] { framework_dir }); + assembly = SearchDirectory (name, new [] { framework_dir }, parameters); if (assembly != null) return assembly; #endif @@ -134,14 +155,14 @@ namespace Mono.Cecil { throw new FileNotFoundException ("Could not resolve: " + name); } - AssemblyDefinition SearchDirectory (AssemblyNameReference name, IEnumerable<string> directories) + AssemblyDefinition SearchDirectory (AssemblyNameReference name, IEnumerable<string> directories, ReaderParameters parameters) { var extensions = new [] { ".exe", ".dll" }; foreach (var directory in directories) { foreach (var extension in extensions) { string file = Path.Combine (directory, name.Name + extension); if (File.Exists (file)) - return GetAssembly (file); + return GetAssembly (file, parameters); } } @@ -150,17 +171,17 @@ namespace Mono.Cecil { static bool IsZero (Version version) { - return version.Major == 0 && version.Minor == 0 && version.Build == 0 && version.Revision == 0; + return version == null || (version.Major == 0 && version.Minor == 0 && version.Build == 0 && version.Revision == 0); } #if !SILVERLIGHT && !CF - AssemblyDefinition GetCorlib (AssemblyNameReference reference) + AssemblyDefinition GetCorlib (AssemblyNameReference reference, ReaderParameters parameters) { var version = reference.Version; var corlib = typeof (object).Assembly.GetName (); if (corlib.Version == version || IsZero (version)) - return GetAssembly (typeof (object).Module.FullyQualifiedName); + return GetAssembly (typeof (object).Module.FullyQualifiedName, parameters); var path = Directory.GetParent ( Directory.GetParent ( @@ -200,7 +221,7 @@ namespace Mono.Cecil { var file = Path.Combine (path, "mscorlib.dll"); if (File.Exists (file)) - return GetAssembly (file); + return GetAssembly (file, parameters); return null; } @@ -246,10 +267,13 @@ namespace Mono.Cecil { static string GetCurrentMonoGac () { - return Path.Combine (Directory.GetParent (typeof (object).Module.FullyQualifiedName).FullName, "gac"); + return Path.Combine ( + Directory.GetParent ( + Path.GetDirectoryName (typeof (object).Module.FullyQualifiedName)).FullName, + "gac"); } - AssemblyDefinition GetAssemblyInGac (AssemblyNameReference reference) + AssemblyDefinition GetAssemblyInGac (AssemblyNameReference reference, ReaderParameters parameters) { if (reference.PublicKeyToken == null || reference.PublicKeyToken.Length == 0) return null; @@ -258,24 +282,24 @@ namespace Mono.Cecil { gac_paths = GetGacPaths (); if (on_mono) - return GetAssemblyInMonoGac (reference); + return GetAssemblyInMonoGac (reference, parameters); - return GetAssemblyInNetGac (reference); + return GetAssemblyInNetGac (reference, parameters); } - AssemblyDefinition GetAssemblyInMonoGac (AssemblyNameReference reference) + AssemblyDefinition GetAssemblyInMonoGac (AssemblyNameReference reference, ReaderParameters parameters) { for (int i = 0; i < gac_paths.Count; i++) { var gac_path = gac_paths [i]; var file = GetAssemblyFile (reference, string.Empty, gac_path); if (File.Exists (file)) - return GetAssembly (file); + return GetAssembly (file, parameters); } return null; } - AssemblyDefinition GetAssemblyInNetGac (AssemblyNameReference reference) + AssemblyDefinition GetAssemblyInNetGac (AssemblyNameReference reference, ReaderParameters parameters) { var gacs = new [] { "GAC_MSIL", "GAC_32", "GAC" }; var prefixes = new [] { string.Empty, "v4.0_" }; @@ -285,7 +309,7 @@ namespace Mono.Cecil { var gac = Path.Combine (gac_paths [i], gacs [j]); var file = GetAssemblyFile (reference, prefixes [i], gac); if (Directory.Exists (gac) && File.Exists (file)) - return GetAssembly (file); + return GetAssembly (file, parameters); } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs index e0216da..bb9f1f9 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EmbeddedResource.cs @@ -78,6 +78,9 @@ namespace Mono.Cecil { public byte [] GetResourceData () { + if (stream != null) + return ReadStream (stream); + if (data != null) return data; @@ -86,5 +89,17 @@ namespace Mono.Cecil { throw new InvalidOperationException (); } + + static byte [] ReadStream (Stream stream) + { + var length = (int) stream.Length; + var data = new byte [length]; + int offset = 0, read; + + while ((read = stream.Read (data, offset, length - offset)) > 0) + offset += read; + + return data; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs index 4c1f1ba..f7bf08c 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs @@ -114,5 +114,15 @@ namespace Mono.Cecil { this.function.Name = "method"; this.etype = MD.ElementType.FnPtr; } + + public override TypeDefinition Resolve () + { + return null; + } + + public override TypeReference GetElementType () + { + return this; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs index 699acad..46c048b 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericInstanceType.cs @@ -52,6 +52,11 @@ namespace Mono.Cecil { } } + public override TypeReference DeclaringType { + get { return ElementType.DeclaringType; } + set { throw new NotSupportedException (); } + } + public override string FullName { get { var name = new StringBuilder (); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs index d9efb5e..d3e5a15 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/GenericParameter.cs @@ -192,5 +192,10 @@ namespace Mono.Cecil { this.owner = owner; this.etype = owner.GenericParameterType == GenericParameterType.Type ? ElementType.Var : ElementType.MVar; } + + public override TypeDefinition Resolve () + { + return null; + } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs index 361fbe4..e3338cf 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/IConstantProvider.cs @@ -30,7 +30,7 @@ namespace Mono.Cecil { public interface IConstantProvider : IMetadataTokenProvider { - bool HasConstant { get; } + bool HasConstant { get; set; } object Constant { get; set; } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs index 52a31b0..93e700e 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/Import.cs @@ -83,11 +83,11 @@ namespace Mono.Cecil { var reference = new TypeReference ( string.Empty, type.Name, + module, ImportScope (type.Assembly), type.IsValueType); reference.etype = ImportElementType (type); - reference.module = module; if (IsNestedType (type)) reference.DeclaringType = ImportType (type.DeclaringType, context, import_kind); @@ -347,11 +347,10 @@ namespace Mono.Cecil { var reference = new TypeReference ( type.Namespace, type.Name, + module, ImportScope (type.Scope), type.IsValueType); - reference.module = module; - MetadataSystem.TryProcessPrimitiveType (reference); if (type.IsNested) @@ -526,8 +525,7 @@ namespace Mono.Cecil { DeclaringType = declaring_type, }; - if (method.IsVarArg ()) - reference.CallingConvention &= MethodCallingConvention.VarArg; + reference.CallingConvention = method.CallingConvention; if (method.HasGenericParameters) ImportGenericParameters (reference, method); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs index 1cfddb9..464fb94 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataResolver.cs @@ -35,7 +35,10 @@ namespace Mono.Cecil { public interface IAssemblyResolver { AssemblyDefinition Resolve (AssemblyNameReference name); + AssemblyDefinition Resolve (AssemblyNameReference name, ReaderParameters parameters); + AssemblyDefinition Resolve (string fullName); + AssemblyDefinition Resolve (string fullName, ReaderParameters parameters); } static class MetadataResolver { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs index a114707..e2196af 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MetadataSystem.cs @@ -100,6 +100,9 @@ namespace Mono.Cecil { public static void TryProcessPrimitiveType (TypeReference type) { var scope = type.scope; + if (scope == null) + return; + if (scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference) return; @@ -368,7 +371,7 @@ namespace Mono.Cecil { return type; } - throw new ArgumentException (); + return null; } } } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs index fff1fb9..3b74e5a 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodDefinition.cs @@ -37,7 +37,7 @@ namespace Mono.Cecil { ushort attributes; ushort impl_attributes; - MethodSemanticsAttributes? sem_attrs; + internal MethodSemanticsAttributes? sem_attrs; Collection<CustomAttribute> custom_attributes; Collection<SecurityDeclaration> security_declarations; @@ -78,18 +78,14 @@ namespace Mono.Cecil { if (sem_attrs.HasValue) return; - var type = DeclaringType; - if (type == null) - return; - - var module = type.Module; + var module = this.Module; if (module == null) return; if (!module.HasImage) return; - sem_attrs = module.Read (this, (method, reader) => reader.ReadMethodSemantics (method)); + module.Read (this, (method, reader) => reader.ReadAllSemantics (method)); } public bool HasSecurityDeclarations { @@ -457,7 +453,22 @@ namespace Mono.Cecil { index--; } - return method.Parameters [index]; + var parameters = method.Parameters; + + if (index < 0 || index >= parameters.size) + return null; + + return parameters [index]; + } + + public static VariableDefinition GetVariable (this MethodBody self, int index) + { + var variables = self.Variables; + + if (index < 0 || index >= variables.size) + return null; + + return variables [index]; } public static bool GetSemantics (this MethodDefinition self, MethodSemanticsAttributes semantics) diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs index 4b571f3..045b9a8 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/MethodReturnType.cs @@ -70,6 +70,7 @@ namespace Mono.Cecil { public bool HasConstant { get { return parameter != null && parameter.HasConstant; } + set { Parameter.HasConstant = value; } } public object Constant { diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs index 597a4b6..7c793f5 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs @@ -182,11 +182,11 @@ namespace Mono.Cecil { internal Image Image; internal MetadataSystem MetadataSystem; internal ReadingMode ReadingMode; - internal IAssemblyResolver AssemblyResolver; internal ISymbolReaderProvider SymbolReaderProvider; internal ISymbolReader SymbolReader; - TypeSystem type_system; + internal IAssemblyResolver assembly_resolver; + internal TypeSystem type_system; readonly MetadataReader reader; readonly string fq_name; @@ -216,6 +216,7 @@ namespace Mono.Cecil { public ModuleKind Kind { get { return kind; } + set { kind = value; } } public TargetRuntime Runtime { @@ -264,6 +265,10 @@ namespace Mono.Cecil { } #endif + public IAssemblyResolver AssemblyResolver { + get { return assembly_resolver; } + } + public TypeSystem TypeSystem { get { return type_system ?? (type_system = TypeSystem.CreateTypeSystem (this)); } } @@ -406,7 +411,7 @@ namespace Mono.Cecil { { this.MetadataSystem = new MetadataSystem (); this.token = new MetadataToken (TokenType.Module, 1); - this.AssemblyResolver = GlobalAssemblyResolver.Instance; + this.assembly_resolver = GlobalAssemblyResolver.Instance; } internal ModuleDefinition (Image image) @@ -431,6 +436,9 @@ namespace Mono.Cecil { { CheckFullName (fullName); + if (!HasImage) + return false; + return Read (this, (_, reader) => reader.GetTypeReference (scope, fullName) != null); } @@ -443,16 +451,27 @@ namespace Mono.Cecil { { CheckFullName (fullName); + if (!HasImage) { + type = null; + return false; + } + return (type = Read (this, (_, reader) => reader.GetTypeReference (scope, fullName))) != null; } public IEnumerable<TypeReference> GetTypeReferences () { + if (!HasImage) + return Empty<TypeReference>.Array; + return Read (this, (_, reader) => reader.GetTypeReferences ()); } public IEnumerable<MemberReference> GetMemberReferences () { + if (!HasImage) + return Empty<MemberReference>.Array; + return Read (this, (_, reader) => reader.GetMemberReferences ()); } @@ -786,7 +805,7 @@ namespace Mono.Cecil { }; if (parameters.AssemblyResolver != null) - module.AssemblyResolver = parameters.AssemblyResolver; + module.assembly_resolver = parameters.AssemblyResolver; if (parameters.Kind != ModuleKind.NetModule) { var assembly = new AssemblyDefinition (); diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs index 11d98b7..0b6e49a 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ModuleReference.cs @@ -56,7 +56,6 @@ namespace Mono.Cecil { public ModuleReference (string name) : this () { - Mixin.CheckName (name); this.name = name; } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs index 8f8d1d5..8c12b54 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterAttributes.cs @@ -35,6 +35,8 @@ namespace Mono.Cecil { None = 0x0000, In = 0x0001, // Param is [In] Out = 0x0002, // Param is [Out] + Lcid = 0x0004, + Retval = 0x0008, Optional = 0x0010, // Param is optional HasDefault = 0x1000, // Param has default value HasFieldMarshal = 0x2000, // Param has field marshal diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs index 2e73a07..059f257 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs @@ -110,6 +110,16 @@ namespace Mono.Cecil { set { attributes = attributes.SetAttributes ((ushort) ParameterAttributes.Out, value); } } + public bool IsLcid { + get { return attributes.GetAttributes ((ushort) ParameterAttributes.Lcid); } + set { attributes = attributes.SetAttributes ((ushort) ParameterAttributes.Lcid, value); } + } + + public bool IsReturnValue { + get { return attributes.GetAttributes ((ushort) ParameterAttributes.Retval); } + set { attributes = attributes.SetAttributes ((ushort) ParameterAttributes.Retval, value); } + } + public bool IsOptional { get { return attributes.GetAttributes ((ushort) ParameterAttributes.Optional); } set { attributes = attributes.SetAttributes ((ushort) ParameterAttributes.Optional, value); } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs index cd3d11a..df6acf3 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeParser.cs @@ -355,9 +355,7 @@ namespace Mono.Cecil { string @namespace, name; SplitFullName (type_info.type_fullname, out @namespace, out name); - var type = new TypeReference (@namespace, name, scope) { - module = module, - }; + var type = new TypeReference (@namespace, name, module, scope); AdjustGenericParameters (type); @@ -366,9 +364,8 @@ namespace Mono.Cecil { return type; for (int i = 0; i < nested_names.Length; i++) { - type = new TypeReference (string.Empty, nested_names [i], null) { + type = new TypeReference (string.Empty, nested_names [i], module, null) { DeclaringType = type, - module = module, }; 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 4ccd86f..e45d823 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeReference.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeReference.cs @@ -33,6 +33,40 @@ using Mono.Collections.Generic; namespace Mono.Cecil { + public enum MetadataType : byte { + Void = ElementType.Void, + Boolean = ElementType.Boolean, + Char = ElementType.Char, + SByte = ElementType.I1, + Byte = ElementType.U1, + Int16 = ElementType.I2, + UInt16 = ElementType.U2, + Int32 = ElementType.I4, + UInt32 = ElementType.U4, + Int64 = ElementType.I8, + UInt64 = ElementType.U8, + Single = ElementType.R4, + Double = ElementType.R8, + String = ElementType.String, + Pointer = ElementType.Ptr, + ByReference = ElementType.ByRef, + ValueType = ElementType.ValueType, + Class = ElementType.Class, + Var = ElementType.Var, + Array = ElementType.Array, + GenericInstance = ElementType.GenericInst, + TypedByReference = ElementType.TypedByRef, + IntPtr = ElementType.I, + UIntPtr = ElementType.U, + FunctionPointer = ElementType.FnPtr, + Object = ElementType.Object, + MVar = ElementType.MVar, + RequiredModifier = ElementType.CModReqD, + OptionalModifier = ElementType.CModOpt, + Sentinel = ElementType.Sentinel, + Pinned = ElementType.Pinned, + } + public class TypeReference : MemberReference, IGenericParameterProvider, IGenericContext { string @namespace; @@ -224,14 +258,15 @@ namespace Mono.Cecil { this.token = new MetadataToken (TokenType.TypeRef, 0); } - public TypeReference (string @namespace, string name, IMetadataScope scope) + public TypeReference (string @namespace, string name, ModuleDefinition module, IMetadataScope scope) : this (@namespace, name) { + this.module = module; this.scope = scope; } - public TypeReference (string @namespace, string name, IMetadataScope scope, bool valueType) : - this (@namespace, name, scope) + public TypeReference (string @namespace, string name, ModuleDefinition module, IMetadataScope scope, bool valueType) : + this (@namespace, name, module, scope) { value_type = valueType; } @@ -272,6 +307,7 @@ namespace Mono.Cecil { case ElementType.Pinned: case ElementType.Ptr: case ElementType.SzArray: + case ElementType.Sentinel: case ElementType.Var: return true; } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs index d484eff..da4e726 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSpecification.cs @@ -77,7 +77,7 @@ namespace Mono.Cecil { this.token = new MetadataToken (TokenType.TypeSpec); } - public sealed override TypeReference GetElementType () + public override TypeReference GetElementType () { return element_type.GetElementType (); } diff --git a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs index 59f72a8..0b0ba91 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeSystem.cs @@ -47,18 +47,21 @@ namespace Mono.Cecil { if (metadata.Types == null) Initialize (module.Types); - var types = metadata.Types; + return module.Read (this, (_, reader) => { + var types = reader.metadata.Types; - for (int i = 0; i < types.Length; i++) { - var type = types [i]; - if (type == null) - continue; + for (int i = 0; i < types.Length; i++) { + if (types [i] == null) + types [i] = reader.GetTypeDefinition ((uint) i + 1); - if (type.Name == name && type.Namespace == @namespace) - return type; - } + var type = types [i]; + + if (type.Name == name && type.Namespace == @namespace) + return type; + } - return null; + return null; + }); } static void Initialize (object obj) @@ -123,9 +126,7 @@ namespace Mono.Cecil { TypeReference CreateTypeReference (string @namespace, string name) { - var type = new TypeReference (@namespace, name, GetCorlibReference ()); - type.module = module; - return type; + return new TypeReference (@namespace, name, module, GetCorlibReference ()); } } 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 9177e39..1137978 100644 --- a/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/Collection.cs +++ b/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Collections.Generic/Collection.cs @@ -113,6 +113,13 @@ namespace Mono.Collections.Generic { items = new T [capacity]; } + public Collection (ICollection<T> items) + { + this.items = new T [items.Count]; + items.CopyTo (this.items, 0); + this.size = this.items.Length; + } + public void Add (T item) { if (size == items.Length) |