diff --git a/Mono.Cecil/.gitignore b/Mono.Cecil/.gitignore index 4b6cda67b..8dcb32b7f 100644 --- a/Mono.Cecil/.gitignore +++ b/Mono.Cecil/.gitignore @@ -7,3 +7,4 @@ obj *.xml *.nupkg **/test-results/* +*Resharper* diff --git a/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs b/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs index 241203438..02876aad5 100644 --- a/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs +++ b/Mono.Cecil/Mono.Cecil.Cil/CodeWriter.cs @@ -519,6 +519,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.Cecil/Mono.Cecil.Cil/Document.cs b/Mono.Cecil/Mono.Cecil.Cil/Document.cs index 6648b7aac..1dbdc4474 100644 --- a/Mono.Cecil/Mono.Cecil.Cil/Document.cs +++ b/Mono.Cecil/Mono.Cecil.Cil/Document.cs @@ -54,6 +54,7 @@ namespace Mono.Cecil.Cil { JScript, Smc, MCpp, + FSharp, } public enum DocumentLanguageVendor { diff --git a/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs b/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs index 4ae749b93..c65c12269 100644 --- a/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs +++ b/Mono.Cecil/Mono.Cecil.Cil/MethodBody.cs @@ -27,6 +27,7 @@ // using System; +using System.Threading; using Mono.Collections.Generic; @@ -100,10 +101,26 @@ 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) + Interlocked.CompareExchange (ref this_parameter, ThisParameterFor (method), null); + + return this_parameter; } } + static ParameterDefinition ThisParameterFor (MethodDefinition method) + { + var declaring_type = method.DeclaringType; + var type = declaring_type.IsValueType || declaring_type.IsPrimitive + ? new PointerType (declaring_type) + : declaring_type as TypeReference; + + return new ParameterDefinition (type, method); + } + public MethodBody (MethodDefinition method) { this.method = method; diff --git a/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs b/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs index 1c54fdb36..b63412dea 100644 --- a/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs +++ b/Mono.Cecil/Mono.Cecil.Metadata/Buffers.cs @@ -274,7 +274,7 @@ namespace Mono.Cecil.Metadata { class StringHeapBuffer : HeapBuffer { - readonly Dictionary strings = new Dictionary (); + readonly Dictionary strings = new Dictionary (StringComparer.Ordinal); public sealed override bool IsEmpty { get { return length <= 1; } diff --git a/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs b/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs index 6f921d9b5..af6aa7a35 100644 --- a/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs +++ b/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs @@ -71,11 +71,11 @@ namespace Mono.Cecil.PE { { this.module = module; this.metadata = metadata; + this.pe64 = module.Architecture != TargetArchitecture.I386; this.GetDebugHeader (); this.GetWin32Resources (); this.text_map = BuildTextMap (); this.sections = 2; // text + reloc - this.pe64 = module.Architecture != TargetArchitecture.I386; this.time_stamp = (uint) DateTime.UtcNow.Subtract (new DateTime (1970, 1, 1)).TotalSeconds; } diff --git a/Mono.Cecil/Mono.Cecil.nuspec b/Mono.Cecil/Mono.Cecil.nuspec index d34d23ca0..021e794e6 100644 --- a/Mono.Cecil/Mono.Cecil.nuspec +++ b/Mono.Cecil/Mono.Cecil.nuspec @@ -2,7 +2,7 @@ Mono.Cecil - 0.9.5.0 + 0.9.5.2 Mono.Cecil Jb Evain Jb Evain diff --git a/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs b/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs index b6fa641ed..8a9f8097e 100644 --- a/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs +++ b/Mono.Cecil/Mono.Cecil/AssemblyInfo.cs @@ -32,7 +32,7 @@ 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)] diff --git a/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs b/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs index 3f8c862bf..9697634f1 100644 --- a/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs +++ b/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs @@ -203,22 +203,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.Cecil/Mono.Cecil/AssemblyReader.cs b/Mono.Cecil/Mono.Cecil/AssemblyReader.cs index 3caa269f8..de296e789 100644 --- a/Mono.Cecil/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Cecil/Mono.Cecil/AssemblyReader.cs @@ -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; } @@ -525,11 +528,10 @@ namespace Mono.Cecil { 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); } @@ -1031,7 +1033,7 @@ namespace Mono.Cecil { type.scope = scope; type.DeclaringType = declaring_type; - MetadataSystem.TryProcessPrimitiveType (type); + MetadataSystem.TryProcessPrimitiveTypeReference (type); return type; } @@ -1796,11 +1798,11 @@ namespace Mono.Cecil { Range range; if (!metadata.TryGetGenericParameterRange (provider, out range) || !MoveTo (Table.GenericParam, range.Start)) - return new Collection (); + return new GenericParameterCollection (provider); metadata.RemoveGenericParameterRange (provider); - var generic_parameters = new Collection ((int) range.Length); + var generic_parameters = new GenericParameterCollection (provider, (int) range.Length); for (uint i = 0; i < range.Length; i++) { ReadUInt16 (); // index @@ -2186,7 +2188,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; @@ -2368,8 +2371,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) @@ -2645,9 +2652,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; @@ -2662,14 +2670,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; @@ -2812,7 +2826,7 @@ namespace Mono.Cecil { method.CallingConvention = (MethodCallingConvention) calling_convention; 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) { diff --git a/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs index bf41d5b72..a2d3ffa41 100644 --- a/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/Mono.Cecil/AssemblyWriter.cs @@ -1458,7 +1458,7 @@ namespace Mono.Cecil { { var pinvoke = method.PInvokeInfo; if (pinvoke == null) - throw new ArgumentException (); + return; var table = GetTable (Table.ImplMap); table.AddRow (new ImplMapRow ( diff --git a/Mono.Cecil/Mono.Cecil/CustomAttribute.cs b/Mono.Cecil/Mono.Cecil/CustomAttribute.cs index 0c12638f9..e8eb05723 100644 --- a/Mono.Cecil/Mono.Cecil/CustomAttribute.cs +++ b/Mono.Cecil/Mono.Cecil/CustomAttribute.cs @@ -188,7 +188,7 @@ namespace Mono.Cecil { if (blob != null) return blob; - if (!HasImage || signature == 0) + if (!HasImage) throw new NotSupportedException (); return Module.Read (ref blob, this, (attribute, reader) => reader.ReadCustomAttributeBlob (attribute.signature)); @@ -210,7 +210,7 @@ namespace Mono.Cecil { fields.Clear (); if (properties != null) properties.Clear (); - + resolved = false; } return this; diff --git a/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs b/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs index 93d249391..a9faa2142 100644 --- a/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs +++ b/Mono.Cecil/Mono.Cecil/DefaultAssemblyResolver.cs @@ -42,7 +42,7 @@ namespace Mono.Cecil { public DefaultAssemblyResolver () { - cache = new Dictionary (); + cache = new Dictionary (StringComparer.Ordinal); } public override AssemblyDefinition Resolve (AssemblyNameReference name) diff --git a/Mono.Cecil/Mono.Cecil/GenericParameter.cs b/Mono.Cecil/Mono.Cecil/GenericParameter.cs index 43891241e..0cbdb36da 100644 --- a/Mono.Cecil/Mono.Cecil/GenericParameter.cs +++ b/Mono.Cecil/Mono.Cecil/GenericParameter.cs @@ -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 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 { @@ -99,15 +100,17 @@ namespace Mono.Cecil { 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; } } public override ModuleDefinition Module { - get { return ((MemberReference) owner).Module; } + get { return module ?? owner.Module; } } public override string Name { @@ -115,7 +118,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; } } @@ -185,8 +188,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 () @@ -194,4 +223,55 @@ namespace Mono.Cecil { return null; } } + + sealed class GenericParameterCollection : Collection { + + 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.Cecil/Mono.Cecil/ICustomAttributeProvider.cs b/Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs index bd7c847dc..86bd3748c 100644 --- a/Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs +++ b/Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs @@ -44,9 +44,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 GetCustomAttributes ( diff --git a/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs b/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs index 0d542b039..7a392b9cc 100644 --- a/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs +++ b/Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs @@ -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 GetGenericParameters ( @@ -70,7 +68,7 @@ namespace Mono.Cecil { { return module.HasImage () ? module.Read (ref collection, self, (provider, reader) => reader.ReadGenericParameters (provider)) - : collection = new Collection (); + : collection = new GenericParameterCollection (self); } } } diff --git a/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs b/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs index e3daeb3c2..e22040092 100644 --- a/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs +++ b/Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs @@ -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.Cecil/Mono.Cecil/Import.cs b/Mono.Cecil/Mono.Cecil/Import.cs index 2a8293c0c..4f3ff25c4 100644 --- a/Mono.Cecil/Mono.Cecil/Import.cs +++ b/Mono.Cecil/Mono.Cecil/Import.cs @@ -351,7 +351,7 @@ namespace Mono.Cecil { ImportScope (type.Scope), type.IsValueType); - MetadataSystem.TryProcessPrimitiveType (reference); + MetadataSystem.TryProcessPrimitiveTypeReference (reference); if (type.IsNested) reference.DeclaringType = ImportType (type.DeclaringType, context); diff --git a/Mono.Cecil/Mono.Cecil/MetadataResolver.cs b/Mono.Cecil/Mono.Cecil/MetadataResolver.cs index 27a215fb8..e69fcd715 100644 --- a/Mono.Cecil/Mono.Cecil/MetadataResolver.cs +++ b/Mono.Cecil/Mono.Cecil/MetadataResolver.cs @@ -27,7 +27,6 @@ // using System; -using System.Collections.Generic; using Mono.Collections.Generic; @@ -41,6 +40,12 @@ namespace Mono.Cecil { AssemblyDefinition Resolve (string fullName, ReaderParameters parameters); } + public interface IMetadataResolver { + TypeDefinition Resolve (TypeReference type); + FieldDefinition Resolve (FieldReference field); + MethodDefinition Resolve (MethodReference method); + } + #if !SILVERLIGHT && !CF [Serializable] #endif @@ -68,29 +73,46 @@ namespace Mono.Cecil { #endif } - static class MetadataResolver { + 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; - return GetType (resolver, assembly.MainModule, type); + return GetType (assembly.MainModule, type); case MetadataScopeType.ModuleDefinition: - return GetType (resolver, (ModuleDefinition) scope, type); + return GetType ((ModuleDefinition) scope, type); case MetadataScopeType.ModuleReference: var modules = type.Module.Assembly.Modules; var module_ref = (ModuleReference) scope; for (int i = 0; i < modules.Count; i++) { var netmodule = modules [i]; if (netmodule.Name == module_ref.Name) - return GetType (resolver, netmodule, type); + return GetType (netmodule, type); } break; } @@ -98,9 +120,9 @@ namespace Mono.Cecil { throw new NotSupportedException (); } - static TypeDefinition GetType (IAssemblyResolver resolver, ModuleDefinition module, TypeReference reference) + static TypeDefinition GetType (ModuleDefinition module, TypeReference reference) { - var type = GetType (module, reference); + var type = GetTypeDefinition (module, reference); if (type != null) return type; @@ -123,7 +145,7 @@ namespace Mono.Cecil { return null; } - static TypeDefinition GetType (ModuleDefinition module, TypeReference type) + static TypeDefinition GetTypeDefinition (ModuleDefinition module, TypeReference type) { if (!type.IsNested) return module.GetType (type.Namespace, type.Name); @@ -135,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); @@ -157,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 fields, FieldReference reference) + static FieldDefinition GetField (Collection fields, FieldReference reference) { for (int i = 0; i < fields.Count; i++) { var field = fields [i]; @@ -180,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; @@ -191,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); @@ -204,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 methods, MethodReference reference) + public static MethodDefinition GetMethod (Collection methods, MethodReference reference) { for (int i = 0; i < methods.Count; i++) { var method = methods [i]; diff --git a/Mono.Cecil/Mono.Cecil/MetadataSystem.cs b/Mono.Cecil/Mono.Cecil/MetadataSystem.cs index bfe7fc58f..d17a78386 100644 --- a/Mono.Cecil/Mono.Cecil/MetadataSystem.cs +++ b/Mono.Cecil/Mono.Cecil/MetadataSystem.cs @@ -78,7 +78,7 @@ namespace Mono.Cecil { static void InitializePrimitives () { - primitive_value_types = new Dictionary> (18) { + primitive_value_types = new Dictionary> (18, StringComparer.Ordinal) { { "Void", new Row (ElementType.Void, false) }, { "Boolean", new Row (ElementType.Boolean, true) }, { "Char", new Row (ElementType.Char, true) }, @@ -100,30 +100,47 @@ namespace Mono.Cecil { }; } - public static void TryProcessPrimitiveType (TypeReference type) + public static void TryProcessPrimitiveTypeReference (TypeReference type) { var scope = type.scope; - if (scope == null) + if (scope == null || scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference || scope.Name != "mscorlib") return; - if (scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference) + Row primitive_data; + if (!TryGetPrimitiveData (type, out primitive_data)) return; - if (scope.Name != "mscorlib") - 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.HasImage || !type.Module.IsCorlib ()) + return false; + + Row 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 primitive_data) + { + primitive_data = new Row (); if (type.Namespace != "System") - return; + return false; if (primitive_value_types == null) InitializePrimitives (); - Row 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 () diff --git a/Mono.Cecil/Mono.Cecil/MethodDefinition.cs b/Mono.Cecil/Mono.Cecil/MethodDefinition.cs index dd9d77e96..90ac6999b 100644 --- a/Mono.Cecil/Mono.Cecil/MethodDefinition.cs +++ b/Mono.Cecil/Mono.Cecil/MethodDefinition.cs @@ -145,7 +145,7 @@ namespace Mono.Cecil { return body = new MethodBody (this); } - set { + set { // we reset Body to null in ILSpy to save memory; so we need that operation to be thread-safe lock (Module.SyncRoot) { body = value; diff --git a/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs b/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs index 94d8771a9..b24fcf704 100644 --- a/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs +++ b/Mono.Cecil/Mono.Cecil/MethodImplAttributes.cs @@ -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.Cecil/Mono.Cecil/MethodReference.cs b/Mono.Cecil/Mono.Cecil/MethodReference.cs index 21de33673..0adab452e 100644 --- a/Mono.Cecil/Mono.Cecil/MethodReference.cs +++ b/Mono.Cecil/Mono.Cecil/MethodReference.cs @@ -99,7 +99,7 @@ namespace Mono.Cecil { if (generic_parameters != null) return generic_parameters; - return generic_parameters = new Collection (); + return generic_parameters = new GenericParameterCollection (this); } } diff --git a/Mono.Cecil/Mono.Cecil/MethodReturnType.cs b/Mono.Cecil/Mono.Cecil/MethodReturnType.cs index f2d3c00f8..c9d260ef1 100644 --- a/Mono.Cecil/Mono.Cecil/MethodReturnType.cs +++ b/Mono.Cecil/Mono.Cecil/MethodReturnType.cs @@ -26,6 +26,8 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.Threading; + using Mono.Collections.Generic; namespace Mono.Cecil { @@ -46,8 +48,12 @@ namespace Mono.Cecil { } internal ParameterDefinition Parameter { - get { return parameter ?? (parameter = new ParameterDefinition (return_type)); } - set { parameter = value; } + get { + if (parameter == null) + Interlocked.CompareExchange (ref parameter, new ParameterDefinition (return_type, method), null); + + return parameter; + } } public MetadataToken MetadataToken { @@ -55,6 +61,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.Cecil/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs index 511a88389..1b191815c 100644 --- a/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs @@ -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; @@ -186,6 +198,7 @@ namespace Mono.Cecil { internal ISymbolReader SymbolReader; internal IAssemblyResolver assembly_resolver; + internal IMetadataResolver metadata_resolver; internal TypeSystem type_system; readonly MetadataReader reader; @@ -263,7 +276,7 @@ namespace Mono.Cecil { internal MetadataImporter MetadataImporter { get { if (importer == null) - Interlocked.CompareExchange(ref importer, new MetadataImporter(this), null); + Interlocked.CompareExchange (ref importer, new MetadataImporter (this), null); return importer; } } @@ -273,10 +286,19 @@ namespace Mono.Cecil { get { return assembly_resolver; } } + public IMetadataResolver MetadataResolver { + get { + if (metadata_resolver == null) + Interlocked.CompareExchange (ref metadata_resolver, new MetadataResolver (assembly_resolver), null); + + return metadata_resolver; + } + } + public TypeSystem TypeSystem { get { if (type_system == null) - Interlocked.CompareExchange(ref type_system, TypeSystem.CreateTypeSystem (this), null); + Interlocked.CompareExchange (ref type_system, TypeSystem.CreateTypeSystem (this), null); return type_system; } } @@ -488,6 +510,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); @@ -555,17 +584,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 @@ -790,7 +819,7 @@ namespace Mono.Cecil { { return Read (token, (t, reader) => reader.LookupToken (t)); } - + readonly object module_lock = new object(); internal object SyncRoot { @@ -811,13 +840,13 @@ namespace Mono.Cecil { return ret; } } - + internal TRet Read (ref TRet variable, TItem item, Func read) where TRet : class { lock (module_lock) { if (variable != null) return variable; - + var position = reader.position; var context = reader.context; @@ -866,10 +895,13 @@ namespace Mono.Cecil { 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; } @@ -878,6 +910,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 () @@ -886,10 +926,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) @@ -993,6 +1033,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.Cecil/Mono.Cecil/ParameterDefinition.cs b/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs index da7bf81bd..1cac8e139 100644 --- a/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs +++ b/Mono.Cecil/Mono.Cecil/ParameterDefinition.cs @@ -138,6 +138,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.Cecil/Mono.Cecil/SecurityDeclaration.cs b/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs index 7f1608bd6..4213ffd3f 100644 --- a/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs +++ b/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs @@ -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 GetSecurityDeclarations ( diff --git a/Mono.Cecil/Mono.Cecil/TypeDefinition.cs b/Mono.Cecil/Mono.Cecil/TypeDefinition.cs index bdb5bcf06..1cfda2ee2 100644 --- a/Mono.Cecil/Mono.Cecil/TypeDefinition.cs +++ b/Mono.Cecil/Mono.Cecil/TypeDefinition.cs @@ -28,6 +28,7 @@ using System; +using Mono.Cecil.Metadata; using Mono.Collections.Generic; namespace Mono.Cecil { @@ -433,6 +434,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.Cecil/Mono.Cecil/TypeParser.cs b/Mono.Cecil/Mono.Cecil/TypeParser.cs index 733c422d2..06dc935a5 100644 --- a/Mono.Cecil/Mono.Cecil/TypeParser.cs +++ b/Mono.Cecil/Mono.Cecil/TypeParser.cs @@ -356,7 +356,7 @@ namespace Mono.Cecil { SplitFullName (type_info.type_fullname, out @namespace, out name); var type = new TypeReference (@namespace, name, module, scope); - MetadataSystem.TryProcessPrimitiveType (type); + MetadataSystem.TryProcessPrimitiveTypeReference (type); AdjustGenericParameters (type); diff --git a/Mono.Cecil/Mono.Cecil/TypeReference.cs b/Mono.Cecil/Mono.Cecil/TypeReference.cs index f4861245f..ff28c636f 100644 --- a/Mono.Cecil/Mono.Cecil/TypeReference.cs +++ b/Mono.Cecil/Mono.Cecil/TypeReference.cs @@ -135,7 +135,7 @@ namespace Mono.Cecil { if (generic_parameters != null) return generic_parameters; - return generic_parameters = new Collection (); + return generic_parameters = new GenericParameterCollection (this); } } @@ -216,28 +216,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 +268,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 diff --git a/Mono.Cecil/Mono.Cecil/TypeSystem.cs b/Mono.Cecil/Mono.Cecil/TypeSystem.cs index f2d079d5a..0dd7701c6 100644 --- a/Mono.Cecil/Mono.Cecil/TypeSystem.cs +++ b/Mono.Cecil/Mono.Cecil/TypeSystem.cs @@ -158,20 +158,12 @@ namespace Mono.Cecil { internal static TypeSystem CreateTypeSystem (ModuleDefinition module) { - if (IsCorlib (module)) + if (module.IsCorlib ()) return new CorlibTypeSystem (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 (ref TypeReference typeRef, string name, ElementType element_type) diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests.csproj b/Mono.Cecil/Test/Mono.Cecil.Tests.csproj index 2bcc896d8..31f297842 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests.csproj +++ b/Mono.Cecil/Test/Mono.Cecil.Tests.csproj @@ -75,17 +75,17 @@ - + False - libs\nunit-2.4.8\nunit.core.dll + libs\nunit-2.5.10\nunit.core.dll - + False - libs\nunit-2.4.8\nunit.core.interfaces.dll + libs\nunit-2.5.10\nunit.core.interfaces.dll - + False - libs\nunit-2.4.8\nunit.framework.dll + libs\nunit-2.5.10\nunit.framework.dll @@ -141,9 +141,14 @@ + + + + + @@ -154,7 +159,9 @@ + + diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/Addin.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/Addin.cs index b8ede12a5..38b02ceca 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/Addin.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/Addin.cs @@ -173,15 +173,17 @@ namespace Mono.Cecil.Tests { return ModuleDefinition.ReadModule (rt_module, reader_parameters); } - public override void RunTestMethod (TestCaseResult testResult) + public override TestResult RunTest () { + var result = new TestResult (TestName); var module = GetModule (); if (module == null) - return; + return result; Reflect.InvokeMethod (Method, Fixture, new object [] { module }); - testResult.Success (); + result.Success (); + return result; } } diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyInfo.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyInfo.cs index 07d2e0bf0..ad7488f77 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyInfo.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyInfo.cs @@ -4,7 +4,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle ("Mono.Cecil.Tests")] [assembly: AssemblyProduct ("Mono.Cecil")] -[assembly: AssemblyCopyright ("Copyright © 2008 - 2010 Jb Evain")] +[assembly: AssemblyCopyright ("Copyright © 2008 - 2011 Jb Evain")] [assembly: ComVisible (false)] diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyTests.cs index 8cd6898fc..3267f77a6 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/AssemblyTests.cs @@ -21,5 +21,15 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (new Version (0, 0, 0, 0), name.Version); Assert.AreEqual (AssemblyHashAlgorithm.SHA1, name.HashAlgorithm); } + + [Test] + public void ParseLowerCaseNameParts() + { + var name = AssemblyNameReference.Parse ("Foo, version=2.0.0.0, culture=fr-FR"); + Assert.AreEqual ("Foo", name.Name); + Assert.AreEqual (2, name.Version.Major); + Assert.AreEqual (0, name.Version.Minor); + Assert.AreEqual ("fr-FR", name.Culture); + } } } diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/CustomAttributesTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/CustomAttributesTests.cs index 6565ba6ca..9682dadbf 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/CustomAttributesTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/CustomAttributesTests.cs @@ -342,6 +342,14 @@ namespace Mono.Cecil.Tests { Assert.AreEqual ("System.Collections.Generic.Dictionary`2[,]>", type.FullName); } + [TestIL ("types.il")] + public void EmptyBlob (ModuleDefinition module) + { + var attribute = module.GetType ("CustomAttribute"); + Assert.AreEqual (1, attribute.CustomAttributes.Count); + Assert.AreEqual (0, attribute.CustomAttributes [0].ConstructorArguments.Count); + } + [Test] public void DefineCustomAttributeFromBlob () { diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/FieldTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/FieldTests.cs index 9596a7bc6..8c6345688 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/FieldTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/FieldTests.cs @@ -245,25 +245,25 @@ namespace Mono.Cecil.Tests { var field = fields.GetField ("int32_int16"); Assert.AreEqual ("System.Int32", field.FieldType.FullName); Assert.IsTrue (field.HasConstant); - Assert.IsInstanceOfType (typeof (short), field.Constant); + Assert.IsInstanceOf (typeof (short), field.Constant); Assert.AreEqual ((short) 1, field.Constant); field = fields.GetField ("int16_int32"); Assert.AreEqual ("System.Int16", field.FieldType.FullName); Assert.IsTrue (field.HasConstant); - Assert.IsInstanceOfType (typeof (int), field.Constant); + Assert.IsInstanceOf (typeof (int), field.Constant); Assert.AreEqual (1, field.Constant); field = fields.GetField ("char_int16"); Assert.AreEqual ("System.Char", field.FieldType.FullName); Assert.IsTrue (field.HasConstant); - Assert.IsInstanceOfType (typeof (short), field.Constant); + Assert.IsInstanceOf (typeof (short), field.Constant); Assert.AreEqual ((short) 1, field.Constant); field = fields.GetField ("int16_char"); Assert.AreEqual ("System.Int16", field.FieldType.FullName); Assert.IsTrue (field.HasConstant); - Assert.IsInstanceOfType (typeof (char), field.Constant); + Assert.IsInstanceOf (typeof (char), field.Constant); Assert.AreEqual ('s', field.Constant); } diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/Formatter.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/Formatter.cs index 967896ce8..a7e1c8704 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/Formatter.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/Formatter.cs @@ -115,6 +115,12 @@ namespace Mono.Cecil.Tests { return; } + var parameter = operand as ParameterDefinition; + if (parameter != null) { + writer.Write (ToInvariantCultureString (parameter.Sequence)); + return; + } + s = ToInvariantCultureString (operand); writer.Write (s); } diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/ImageReadTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/ImageReadTests.cs index c601fc986..e77bfe066 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/ImageReadTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/ImageReadTests.cs @@ -88,40 +88,32 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (1, heap [Table.AssemblyRef].Length); } - [Test] - public void ReadX64Image () + [TestModule ("hello.x64.exe")] + public void X64Module (ModuleDefinition module) { - var image = GetResourceImage ("hello.x64.exe"); - - Assert.AreEqual (TargetArchitecture.AMD64, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + Assert.AreEqual (TargetArchitecture.AMD64, module.Image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes); } - [Test] - public void ReadIA64Image () + [TestModule ("hello.ia64.exe")] + public void IA64Module (ModuleDefinition module) { - var image = GetResourceImage ("hello.ia64.exe"); - - Assert.AreEqual (TargetArchitecture.IA64, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + Assert.AreEqual (TargetArchitecture.IA64, module.Image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes); } - [Test] - public void ReadX86Image () + [TestModule ("hello.x86.exe")] + public void X86Module (ModuleDefinition module) { - var image = GetResourceImage ("hello.x86.exe"); - - Assert.AreEqual (TargetArchitecture.I386, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly | ModuleAttributes.Required32Bit, image.Attributes); + Assert.AreEqual (TargetArchitecture.I386, module.Image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly | ModuleAttributes.Required32Bit, module.Image.Attributes); } - [Test] - public void ReadAnyCpuImage () + [TestModule ("hello.anycpu.exe")] + public void AnyCpuModule (ModuleDefinition module) { - var image = GetResourceImage ("hello.anycpu.exe"); - - Assert.AreEqual (TargetArchitecture.I386, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + Assert.AreEqual (TargetArchitecture.I386, module.Image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes); } } } diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/MethodBodyTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/MethodBodyTests.cs index afe8f79f9..5258305a9 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/MethodBodyTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/MethodBodyTests.cs @@ -201,9 +201,58 @@ namespace Mono.Cecil.Tests { IL_0005: ret ", method); + Assert.AreEqual (method.Body.ThisParameter.ParameterType, type); Assert.AreEqual (method.Body.ThisParameter, method.Body.Instructions [0].Operand); } + [Test] + public void ThisParameterStaticMethod () + { + var static_method = typeof (ModuleDefinition).ToDefinition ().Methods.Where (m => m.IsStatic).First (); + Assert.IsNull (static_method.Body.ThisParameter); + } + + [Test] + public void ThisParameterPrimitive () + { + var int32 = typeof (int).ToDefinition (); + var int_to_string = int32.Methods.Where (m => m.Name == "ToString" && m.Parameters.Count == 0).First(); + Assert.IsNotNull (int_to_string); + + var this_parameter_type = int_to_string.Body.ThisParameter.ParameterType; + Assert.IsTrue (this_parameter_type.IsPointer); + + var element_type = ((PointerType) this_parameter_type).ElementType; + Assert.AreEqual (int32, element_type); + } + + [Test] + public void ThisParameterValueType () + { + var token = typeof (MetadataToken).ToDefinition (); + var token_to_string = token.Methods.Where (m => m.Name == "ToString" && m.Parameters.Count == 0).First (); + Assert.IsNotNull (token_to_string); + + var this_parameter_type = token_to_string.Body.ThisParameter.ParameterType; + Assert.IsTrue (this_parameter_type.IsPointer); + + var element_type = ((PointerType) this_parameter_type).ElementType; + Assert.AreEqual (token, element_type); + } + + [Test] + public void ThisParameterObject () + { + var module = typeof (MethodBodyTests).ToDefinition ().Module; + var @object = module.TypeSystem.Object.Resolve (); + var method = @object.Methods.Where (m => m.HasBody).First (); + + var type = method.Body.ThisParameter.ParameterType; + Assert.IsFalse (type.IsValueType); + Assert.IsFalse (type.IsPrimitive); + Assert.IsFalse (type.IsPointer); + } + [TestIL ("hello.il")] public void FilterMaxStack (ModuleDefinition module) { diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/MethodTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/MethodTests.cs index c7d55f9a3..5d4d3751a 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/MethodTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/MethodTests.cs @@ -189,5 +189,13 @@ namespace Mono.Cecil.Tests { Assert.AreEqual ("System.Collections.Generic.List`1", new_list_beta.DeclaringType.FullName); Assert.AreEqual ("System.Collections.Generic.List`1", new_list_charlie.DeclaringType.FullName); } + + [Test] + public void ReturnParameterMethod () + { + var method = typeof (MethodTests).ToDefinition ().GetMethod ("ReturnParameterMethod"); + Assert.IsNotNull (method); + Assert.AreEqual (method, method.MethodReturnType.Parameter.Method); + } } } diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/ModuleTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/ModuleTests.cs index ba9ebfc0e..00f9e1af6 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/ModuleTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/ModuleTests.cs @@ -12,6 +12,16 @@ namespace Mono.Cecil.Tests { [TestFixture] public class ModuleTests : BaseTestFixture { + [Test] + public void CreateModuleEscapesAssemblyName () + { + var module = ModuleDefinition.CreateModule ("Test.dll", ModuleKind.Dll); + Assert.AreEqual ("Test", module.Assembly.Name.Name); + + module = ModuleDefinition.CreateModule ("Test.exe", ModuleKind.Console); + Assert.AreEqual ("Test", module.Assembly.Name.Name); + } + [TestModule ("hello.exe")] public void SingleModule (ModuleDefinition module) { diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/SecurityDeclarationTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/SecurityDeclarationTests.cs index 0175c6c82..2cc8d5b43 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/SecurityDeclarationTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/SecurityDeclarationTests.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.IO; using System.Linq; using System.Text; @@ -88,6 +89,39 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (permission_set, argument.Value); } + [Test] + public void DefineSecurityDeclarationByBlob () + { + var file = Path.Combine(Path.GetTempPath(), "SecDecBlob.dll"); + var module = ModuleDefinition.CreateModule ("SecDecBlob.dll", new ModuleParameters { Kind = ModuleKind.Dll, Runtime = TargetRuntime.Net_2_0 }); + + const string permission_set = "\r\n\r\n\r\n"; + + var declaration = new SecurityDeclaration (SecurityAction.Deny, Encoding.Unicode.GetBytes (permission_set)); + module.Assembly.SecurityDeclarations.Add (declaration); + + module.Write (file); + module = ModuleDefinition.ReadModule (file); + + declaration = module.Assembly.SecurityDeclarations [0]; + Assert.AreEqual (SecurityAction.Deny, declaration.Action); + Assert.AreEqual (1, declaration.SecurityAttributes.Count); + + var attribute = declaration.SecurityAttributes [0]; + Assert.AreEqual ("System.Security.Permissions.PermissionSetAttribute", attribute.AttributeType.FullName); + Assert.AreEqual (1, attribute.Properties.Count); + + var named_argument = attribute.Properties [0]; + Assert.AreEqual ("XML", named_argument.Name); + var argument = named_argument.Argument; + Assert.AreEqual ("System.String", argument.Type.FullName); + Assert.AreEqual (permission_set, argument.Value); + } + [TestModule ("empty-decsec-att.dll")] public void SecurityDeclarationWithoutAttributes (ModuleDefinition module) { diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/TypeParserTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/TypeParserTests.cs index a367919b9..6b52e98a9 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/TypeParserTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/TypeParserTests.cs @@ -24,7 +24,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual ("String", type.Name); Assert.AreEqual (MetadataType.String, type.MetadataType); Assert.IsFalse (type.IsValueType); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); } [Test] @@ -43,7 +43,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual ("Int32", type.Name); Assert.AreEqual (MetadataType.Int32, type.MetadataType); Assert.IsTrue (type.IsValueType); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); } [Test] @@ -59,7 +59,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("Mono.Cecil.Tests", type.Namespace); Assert.AreEqual ("TypeParserTests", type.Name); - Assert.IsInstanceOfType (typeof (TypeDefinition), type); + Assert.IsInstanceOf (typeof (TypeDefinition), type); } [Test] @@ -72,7 +72,7 @@ namespace Mono.Cecil.Tests { var type = TypeParser.ParseType (module, fullname); - Assert.IsInstanceOfType (typeof (ByReferenceType), type); + Assert.IsInstanceOf (typeof (ByReferenceType), type); type = ((ByReferenceType) type).ElementType; @@ -81,7 +81,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("System", type.Namespace); Assert.AreEqual ("String", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); } [Test] @@ -98,7 +98,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("Mono.Cecil", type.Namespace); Assert.AreEqual ("TypeDefinition", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); } [Test] @@ -115,7 +115,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("System.Collections.Generic", type.Namespace); Assert.AreEqual ("Dictionary`2", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); Assert.AreEqual (2, type.GenericParameters.Count); } @@ -156,7 +156,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("", type.Namespace); Assert.AreEqual ("Baz`1", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); Assert.AreEqual (1, type.GenericParameters.Count); type = type.DeclaringType; @@ -166,7 +166,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("", type.Namespace); Assert.AreEqual ("Bar`1", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); Assert.AreEqual (1, type.GenericParameters.Count); type = type.DeclaringType; @@ -176,7 +176,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("Bingo", type.Namespace); Assert.AreEqual ("Foo`1", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); Assert.AreEqual (1, type.GenericParameters.Count); } @@ -203,7 +203,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("Bingo", type.Namespace); Assert.AreEqual ("Gazonk", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); } [Test] @@ -227,7 +227,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, type.Module); Assert.AreEqual ("Bingo", type.Namespace); Assert.AreEqual ("Gazonk", type.Name); - Assert.IsInstanceOfType (typeof (TypeReference), type); + Assert.IsInstanceOf (typeof (TypeReference), type); } [Test] @@ -290,7 +290,7 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (2, type.GenericParameters.Count); var argument = instance.GenericArguments [0]; - Assert.IsInstanceOfType (typeof (TypeDefinition), argument); + Assert.IsInstanceOf (typeof (TypeDefinition), argument); Assert.AreEqual (module, argument.Module); Assert.AreEqual ("Mono.Cecil.Tests", argument.Namespace); Assert.AreEqual ("TypeParserTests", argument.Name); @@ -332,13 +332,13 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (module, argument.Module); Assert.AreEqual ("", argument.Namespace); Assert.AreEqual ("Bar", argument.Name); - Assert.IsInstanceOfType (typeof (TypeDefinition), argument); + Assert.IsInstanceOf (typeof (TypeDefinition), argument); argument = instance.GenericArguments [1]; Assert.AreEqual (module, argument.Module); Assert.AreEqual ("", argument.Namespace); Assert.AreEqual ("Bar", argument.Name); - Assert.IsInstanceOfType (typeof (TypeDefinition), argument); + Assert.IsInstanceOf (typeof (TypeDefinition), argument); } [Test] @@ -376,13 +376,13 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (2, instance.GenericArguments.Count); Assert.AreEqual (module, instance.Module); Assert.AreEqual ("Mono.Cecil.Tests.TypeParserTests/Foo`2", instance.ElementType.FullName); - Assert.IsInstanceOfType (typeof (TypeDefinition), instance.ElementType); + Assert.IsInstanceOf (typeof (TypeDefinition), instance.ElementType); argument = instance.GenericArguments [0]; Assert.AreEqual (module, argument.Module); Assert.AreEqual ("Mono.Cecil.Tests", argument.Namespace); Assert.AreEqual ("TypeParserTests", argument.Name); - Assert.IsInstanceOfType (typeof (TypeDefinition), argument); + Assert.IsInstanceOf (typeof (TypeDefinition), argument); argument = instance.GenericArguments [1]; Assert.AreEqual ("mscorlib", argument.Scope.Name); diff --git a/Mono.Cecil/Test/Mono.Cecil.Tests/TypeTests.cs b/Mono.Cecil/Test/Mono.Cecil.Tests/TypeTests.cs index f6b925ee7..878885b4a 100644 --- a/Mono.Cecil/Test/Mono.Cecil.Tests/TypeTests.cs +++ b/Mono.Cecil/Test/Mono.Cecil.Tests/TypeTests.cs @@ -167,5 +167,47 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (1, instance.GenericArguments.Count); Assert.AreEqual (1, owner.GenericParameters.Count); } + + [TestModule ("cscgpbug.dll", Verify = false)] + public void UnboundGenericParameter (ModuleDefinition module) + { + var type = module.GetType ("ListViewModel"); + var method = type.GetMethod ("<>n__FabricatedMethod1"); + + var parameter = method.ReturnType as GenericParameter; + + Assert.IsNotNull (parameter); + Assert.AreEqual (0, parameter.Position); + Assert.IsNull (parameter.Owner); + } + + [TestCSharp ("Generics.cs")] + public void GenericMultidimensionalArray (ModuleDefinition module) + { + var type = module.GetType ("LaMatrix"); + var method = type.GetMethod ("At"); + + var call = method.Body.Instructions.Where (i => i.Operand is MethodReference).First (); + var get = (MethodReference) call.Operand; + + Assert.IsNotNull (get); + Assert.AreEqual (0, get.GenericParameters.Count); + Assert.AreEqual (MethodCallingConvention.Default, get.CallingConvention); + Assert.AreEqual (method.GenericParameters [0], get.ReturnType); + } + + [Test] + public void CorlibPrimitive () + { + var module = typeof (TypeTests).ToDefinition ().Module; + + var int32 = module.TypeSystem.Int32; + Assert.IsTrue (int32.IsPrimitive); + Assert.AreEqual (MetadataType.Int32, int32.MetadataType); + + var int32_def = int32.Resolve (); + Assert.IsTrue (int32_def.IsPrimitive); + Assert.AreEqual (MetadataType.Int32, int32_def.MetadataType); + } } } diff --git a/Mono.Cecil/Test/Resources/assemblies/cscgpbug.dll b/Mono.Cecil/Test/Resources/assemblies/cscgpbug.dll new file mode 100644 index 000000000..5a5c0f3f4 Binary files /dev/null and b/Mono.Cecil/Test/Resources/assemblies/cscgpbug.dll differ diff --git a/Mono.Cecil/Test/Resources/cs/Generics.cs b/Mono.Cecil/Test/Resources/cs/Generics.cs index 5c7d5adda..3f6b1f966 100644 --- a/Mono.Cecil/Test/Resources/cs/Generics.cs +++ b/Mono.Cecil/Test/Resources/cs/Generics.cs @@ -112,3 +112,10 @@ public class DoubleFuncClass { public void Test () { Test (); Test (); } public void Test () { Test (); Test (); } } + +public class LaMatrix { + public static T At (T[,] m, int i, int j) + { + return m [i, j]; + } +} diff --git a/Mono.Cecil/Test/Resources/il/types.il b/Mono.Cecil/Test/Resources/il/types.il index 4c9acafb5..b230c2182 100644 --- a/Mono.Cecil/Test/Resources/il/types.il +++ b/Mono.Cecil/Test/Resources/il/types.il @@ -44,3 +44,15 @@ .field private static literal int16 int16_char = char(0x0073) .field private static literal int32 int32_nullref = nullref } + +.class public auto ansi CustomAttribute extends [mscorlib]System.Attribute +{ + .custom instance void CustomAttribute::.ctor() = () + + .method public hidebysig specialname rtspecialname instance void .ctor() + { + ldarg.0 + call instance void [mscorlib]System.Attribute::.ctor() + ret + } +} diff --git a/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.core.dll b/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.core.dll deleted file mode 100755 index a31edfdd9..000000000 Binary files a/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.core.dll and /dev/null differ diff --git a/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.core.interfaces.dll b/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.core.interfaces.dll deleted file mode 100755 index 2c48764c8..000000000 Binary files a/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.core.interfaces.dll and /dev/null differ diff --git a/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.framework.dll b/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.framework.dll deleted file mode 100755 index 2a0a0aa32..000000000 Binary files a/Mono.Cecil/Test/libs/nunit-2.4.8/nunit.framework.dll and /dev/null differ diff --git a/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.core.dll b/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.core.dll new file mode 100755 index 000000000..a1dd69866 Binary files /dev/null and b/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.core.dll differ diff --git a/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.core.interfaces.dll b/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.core.interfaces.dll new file mode 100755 index 000000000..0ac878820 Binary files /dev/null and b/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.core.interfaces.dll differ diff --git a/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.framework.dll b/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.framework.dll new file mode 100755 index 000000000..6856e51ef Binary files /dev/null and b/Mono.Cecil/Test/libs/nunit-2.5.10/nunit.framework.dll differ diff --git a/Mono.Cecil/rocks/Mono.Cecil.Rocks/AssemblyInfo.cs b/Mono.Cecil/rocks/Mono.Cecil.Rocks/AssemblyInfo.cs index 550a476bb..90543bb5e 100644 --- a/Mono.Cecil/rocks/Mono.Cecil.Rocks/AssemblyInfo.cs +++ b/Mono.Cecil/rocks/Mono.Cecil.Rocks/AssemblyInfo.cs @@ -32,7 +32,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle ("Mono.Cecil.Rocks")] [assembly: AssemblyProduct ("Mono.Cecil")] -[assembly: AssemblyCopyright ("Copyright © 2008 - 2010 Jb Evain")] +[assembly: AssemblyCopyright ("Copyright © 2008 - 2011 Jb Evain")] [assembly: CLSCompliant (false)] [assembly: ComVisible (false)] diff --git a/Mono.Cecil/rocks/Test/Mono.Cecil.Rocks.Tests.csproj b/Mono.Cecil/rocks/Test/Mono.Cecil.Rocks.Tests.csproj index a8a8c912b..3e7b8deb3 100644 --- a/Mono.Cecil/rocks/Test/Mono.Cecil.Rocks.Tests.csproj +++ b/Mono.Cecil/rocks/Test/Mono.Cecil.Rocks.Tests.csproj @@ -51,17 +51,17 @@ v4.0 - + False - ..\..\Test\libs\nunit-2.4.8\nunit.core.dll + ..\..\Test\libs\nunit-2.5.10\nunit.core.dll - + False - ..\..\Test\libs\nunit-2.4.8\nunit.core.interfaces.dll + ..\..\Test\libs\nunit-2.5.10\nunit.core.interfaces.dll - + False - ..\..\Test\libs\nunit-2.4.8\nunit.framework.dll + ..\..\Test\libs\nunit-2.5.10\nunit.framework.dll diff --git a/Mono.Cecil/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs b/Mono.Cecil/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs index 3845eaa55..9f51cb634 100644 --- a/Mono.Cecil/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs +++ b/Mono.Cecil/rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs @@ -16,7 +16,7 @@ namespace Mono.Cecil.Tests { var string_array = @string.MakeArrayType (); - Assert.IsInstanceOfType (typeof (ArrayType), string_array); + Assert.IsInstanceOf (typeof (ArrayType), string_array); Assert.AreEqual (1, string_array.Rank); } @@ -27,7 +27,7 @@ namespace Mono.Cecil.Tests { var string_array = @string.MakeArrayType (3); - Assert.IsInstanceOfType (typeof (ArrayType), string_array); + Assert.IsInstanceOf (typeof (ArrayType), string_array); Assert.AreEqual (3, string_array.Rank); } @@ -38,7 +38,7 @@ namespace Mono.Cecil.Tests { var string_ptr = @string.MakePointerType (); - Assert.IsInstanceOfType (typeof (PointerType), string_ptr); + Assert.IsInstanceOf (typeof (PointerType), string_ptr); } [Test] @@ -48,7 +48,7 @@ namespace Mono.Cecil.Tests { var string_byref = @string.MakeByReferenceType (); - Assert.IsInstanceOfType (typeof (ByReferenceType), string_byref); + Assert.IsInstanceOf (typeof (ByReferenceType), string_byref); } class OptionalModifier {} @@ -61,7 +61,7 @@ namespace Mono.Cecil.Tests { var string_modopt = @string.MakeOptionalModifierType (modopt); - Assert.IsInstanceOfType (typeof (OptionalModifierType), string_modopt); + Assert.IsInstanceOf (typeof (OptionalModifierType), string_modopt); Assert.AreEqual (modopt, string_modopt.ModifierType); } @@ -75,7 +75,7 @@ namespace Mono.Cecil.Tests { var string_modreq = @string.MakeRequiredModifierType (modreq); - Assert.IsInstanceOfType (typeof (RequiredModifierType), string_modreq); + Assert.IsInstanceOf (typeof (RequiredModifierType), string_modreq); Assert.AreEqual (modreq, string_modreq.ModifierType); } @@ -86,7 +86,7 @@ namespace Mono.Cecil.Tests { var pinned_byte_array = byte_array.MakePinnedType (); - Assert.IsInstanceOfType (typeof (PinnedType), pinned_byte_array); + Assert.IsInstanceOf (typeof (PinnedType), pinned_byte_array); } [Test] @@ -96,7 +96,7 @@ namespace Mono.Cecil.Tests { var string_sentinel = @string.MakeSentinelType (); - Assert.IsInstanceOfType (typeof (SentinelType), string_sentinel); + Assert.IsInstanceOf (typeof (SentinelType), string_sentinel); } class Foo {} @@ -110,7 +110,7 @@ namespace Mono.Cecil.Tests { var foo_string_int = foo.MakeGenericInstanceType (@string, @int); - Assert.IsInstanceOfType (typeof (GenericInstanceType), foo_string_int); + Assert.IsInstanceOf (typeof (GenericInstanceType), foo_string_int); Assert.AreEqual (2, foo_string_int.GenericArguments.Count); Assert.AreEqual (@string, foo_string_int.GenericArguments [0]); Assert.AreEqual (@int, foo_string_int.GenericArguments [1]); diff --git a/Mono.Cecil/rocks/Test/Resources/cs/Types.cs b/Mono.Cecil/rocks/Test/Resources/cs/Types.cs old mode 100755 new mode 100644 diff --git a/Mono.Cecil/symbols/mdb/Mono.Cecil.Mdb/AssemblyInfo.cs b/Mono.Cecil/symbols/mdb/Mono.Cecil.Mdb/AssemblyInfo.cs index 384513a23..8186b506b 100644 --- a/Mono.Cecil/symbols/mdb/Mono.Cecil.Mdb/AssemblyInfo.cs +++ b/Mono.Cecil/symbols/mdb/Mono.Cecil.Mdb/AssemblyInfo.cs @@ -32,7 +32,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle ("Mono.Cecil.Mdb")] [assembly: AssemblyProduct ("Mono.Cecil")] -[assembly: AssemblyCopyright ("Copyright © 2008 - 2010 Jb Evain")] +[assembly: AssemblyCopyright ("Copyright © 2008 - 2011 Jb Evain")] [assembly: CLSCompliant (false)] [assembly: ComVisible (false)] diff --git a/Mono.Cecil/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj b/Mono.Cecil/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj index ef5b01f95..708f8009f 100644 --- a/Mono.Cecil/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj +++ b/Mono.Cecil/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj @@ -95,17 +95,17 @@ - + False - ..\..\..\Test\libs\nunit-2.4.8\nunit.core.dll + ..\..\..\Test\libs\nunit-2.5.10\nunit.core.dll - + False - ..\..\..\Test\libs\nunit-2.4.8\nunit.core.interfaces.dll + ..\..\..\Test\libs\nunit-2.5.10\nunit.core.interfaces.dll - + False - ..\..\..\Test\libs\nunit-2.4.8\nunit.framework.dll + ..\..\..\Test\libs\nunit-2.5.10\nunit.framework.dll diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitAccess.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitAccess.cs index 852f79793..e8e77b0bc 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitAccess.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitAccess.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -12,7 +17,6 @@ namespace Microsoft.Cci.Pdb { internal BitAccess(int capacity) { this.buffer = new byte[capacity]; - this.offset = 0; } internal byte[] Buffer { @@ -26,15 +30,26 @@ namespace Microsoft.Cci.Pdb { offset = 0; } + internal void Append(Stream stream, int count) { + int newCapacity = offset + count; + if (buffer.Length < newCapacity) { + byte[] newBuffer = new byte[newCapacity]; + Array.Copy(buffer, newBuffer, buffer.Length); + buffer = newBuffer; + } + stream.Read(buffer, offset, count); + offset += count; + } + internal int Position { get { return offset; } set { offset = value; } } private int offset; - internal void WriteBuffer(Stream stream, int count) { - stream.Write(buffer, 0, count); - } + //internal void WriteBuffer(Stream stream, int count) { + // stream.Write(buffer, 0, count); + //} internal void MinCapacity(int capacity) { if (buffer.Length < capacity) { @@ -49,25 +64,25 @@ namespace Microsoft.Cci.Pdb { } } - internal void WriteInt32(int value) { - buffer[offset + 0] = (byte)value; - buffer[offset + 1] = (byte)(value >> 8); - buffer[offset + 2] = (byte)(value >> 16); - buffer[offset + 3] = (byte)(value >> 24); - offset += 4; - } + //internal void WriteInt32(int value) { + // buffer[offset + 0] = (byte)value; + // buffer[offset + 1] = (byte)(value >> 8); + // buffer[offset + 2] = (byte)(value >> 16); + // buffer[offset + 3] = (byte)(value >> 24); + // offset += 4; + //} - internal void WriteInt32(int[] values) { - for (int i = 0; i < values.Length; i++) { - WriteInt32(values[i]); - } - } + //internal void WriteInt32(int[] values) { + // for (int i = 0; i < values.Length; i++) { + // WriteInt32(values[i]); + // } + //} - internal void WriteBytes(byte[] bytes) { - for (int i = 0; i < bytes.Length; i++) { - buffer[offset++] = bytes[i]; - } - } + //internal void WriteBytes(byte[] bytes) { + // for (int i = 0; i < bytes.Length; i++) { + // buffer[offset++] = bytes[i]; + // } + //} internal void ReadInt16(out short value) { value = (short)((buffer[offset + 0] & 0xFF) | @@ -75,6 +90,11 @@ namespace Microsoft.Cci.Pdb { offset += 2; } + internal void ReadInt8(out sbyte value) { + value = (sbyte)buffer[offset]; + offset += 1; + } + internal void ReadInt32(out int value) { value = (int)((buffer[offset + 0] & 0xFF) | (buffer[offset + 1] << 8) | @@ -84,14 +104,14 @@ namespace Microsoft.Cci.Pdb { } internal void ReadInt64(out long value) { - value = (long)((buffer[offset + 0] & 0xFF) | - (buffer[offset + 1] << 8) | - (buffer[offset + 2] << 16) | - (buffer[offset + 3] << 24) | - (buffer[offset + 4] << 32) | - (buffer[offset + 5] << 40) | - (buffer[offset + 6] << 48) | - (buffer[offset + 7] << 56)); + value = (long)(((ulong)buffer[offset + 0] & 0xFF) | + ((ulong)buffer[offset + 1] << 8) | + ((ulong)buffer[offset + 2] << 16) | + ((ulong)buffer[offset + 3] << 24) | + ((ulong)buffer[offset + 4] << 32) | + ((ulong)buffer[offset + 5] << 40) | + ((ulong)buffer[offset + 6] << 48) | + ((ulong)buffer[offset + 7] << 56)); offset += 8; } @@ -115,14 +135,14 @@ namespace Microsoft.Cci.Pdb { } internal void ReadUInt64(out ulong value) { - value = (ulong)((buffer[offset + 0] & 0xFF) | - (buffer[offset + 1] << 8) | - (buffer[offset + 2] << 16) | - (buffer[offset + 3] << 24) | - (buffer[offset + 4] << 32) | - (buffer[offset + 5] << 40) | - (buffer[offset + 6] << 48) | - (buffer[offset + 7] << 56)); + value = (ulong)(((ulong)buffer[offset + 0] & 0xFF) | + ((ulong)buffer[offset + 1] << 8) | + ((ulong)buffer[offset + 2] << 16) | + ((ulong)buffer[offset + 3] << 24) | + ((ulong)buffer[offset + 4] << 32) | + ((ulong)buffer[offset + 5] << 40) | + ((ulong)buffer[offset + 6] << 48) | + ((ulong)buffer[offset + 7] << 56)); offset += 8; } @@ -159,7 +179,7 @@ namespace Microsoft.Cci.Pdb { internal decimal ReadDecimal() { int[] bits = new int[4]; this.ReadInt32(bits); - return new decimal(bits); + return new decimal(bits[2], bits[3], bits[1], bits[0] < 0, (byte)((bits[0] & 0x00FF0000) >> 16)); } internal void ReadBString(out string value) { diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitSet.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitSet.cs index a9ed7f76e..671913171 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitSet.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/BitSet.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -13,10 +18,10 @@ namespace Microsoft.Cci.Pdb { bits.ReadUInt32(words); } - internal BitSet(int size) { - this.size = size; - words = new uint[size]; - } + //internal BitSet(int size) { + // this.size = size; + // words = new uint[size]; + //} internal bool IsSet(int index) { int word = index / 32; @@ -24,43 +29,43 @@ namespace Microsoft.Cci.Pdb { return ((words[word] & GetBit(index)) != 0); } - internal void Set(int index) { - int word = index / 32; - if (word >= this.size) return; - words[word] |= GetBit(index); - } + //internal void Set(int index) { + // int word = index / 32; + // if (word >= this.size) return; + // words[word] |= GetBit(index); + //} - internal void Clear(int index) { - int word = index / 32; - if (word >= this.size) return; - words[word] &= ~GetBit(index); - } + //internal void Clear(int index) { + // int word = index / 32; + // if (word >= this.size) return; + // words[word] &= ~GetBit(index); + //} - private uint GetBit(int index) { + private static uint GetBit(int index) { return ((uint)1 << (index % 32)); } - private uint ReverseBits(uint value) { - uint o = 0; - for (int i = 0; i < 32; i++) { - o = (o << 1) | (value & 1); - value >>= 1; - } - return o; - } + //private static uint ReverseBits(uint value) { + // uint o = 0; + // for (int i = 0; i < 32; i++) { + // o = (o << 1) | (value & 1); + // value >>= 1; + // } + // return o; + //} internal bool IsEmpty { get { return size == 0; } } - internal bool GetWord(int index, out uint word) { - if (index < size) { - word = ReverseBits(words[index]); - return true; - } - word = 0; - return false; - } + //internal bool GetWord(int index, out uint word) { + // if (index < size) { + // word = ReverseBits(words[index]); + // return true; + // } + // word = 0; + // return false; + //} private int size; private uint[] words; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/CvInfo.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/CvInfo.cs index bef308653..49c51ce1a 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/CvInfo.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/CvInfo.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- // @@ -163,67 +168,67 @@ namespace Microsoft.Cci.Pdb { // function to extract primitive mode, type and size - internal static CV_prmode CV_MODE(TYPE_ENUM typ) { - return (CV_prmode)((((uint)typ) & CV_MMASK) >> CV_MSHIFT); - } + //internal static CV_prmode CV_MODE(TYPE_ENUM typ) { + // return (CV_prmode)((((uint)typ) & CV_MMASK) >> CV_MSHIFT); + //} - internal static CV_type CV_TYPE(TYPE_ENUM typ) { - return (CV_type)((((uint)typ) & CV_TMASK) >> CV_TSHIFT); - } + //internal static CV_type CV_TYPE(TYPE_ENUM typ) { + // return (CV_type)((((uint)typ) & CV_TMASK) >> CV_TSHIFT); + //} - internal static uint CV_SUBT(TYPE_ENUM typ) { - return ((((uint)typ) & CV_SMASK) >> CV_SSHIFT); - } + //internal static uint CV_SUBT(TYPE_ENUM typ) { + // return ((((uint)typ) & CV_SMASK) >> CV_SSHIFT); + //} // functions to check the type of a primitive - internal static bool CV_TYP_IS_DIRECT(TYPE_ENUM typ) { - return (CV_MODE(typ) == CV_prmode.CV_TM_DIRECT); - } - - internal static bool CV_TYP_IS_PTR(TYPE_ENUM typ) { - return (CV_MODE(typ) != CV_prmode.CV_TM_DIRECT); - } - - internal static bool CV_TYP_IS_SIGNED(TYPE_ENUM typ) { - return - (((CV_TYPE(typ) == CV_type.CV_SIGNED) && CV_TYP_IS_DIRECT(typ)) || - (typ == TYPE_ENUM.T_INT1) || - (typ == TYPE_ENUM.T_INT2) || - (typ == TYPE_ENUM.T_INT4) || - (typ == TYPE_ENUM.T_INT8) || - (typ == TYPE_ENUM.T_INT16) || - (typ == TYPE_ENUM.T_RCHAR)); - } - - internal static bool CV_TYP_IS_UNSIGNED(TYPE_ENUM typ) { - return (((CV_TYPE(typ) == CV_type.CV_UNSIGNED) && CV_TYP_IS_DIRECT(typ)) || - (typ == TYPE_ENUM.T_UINT1) || - (typ == TYPE_ENUM.T_UINT2) || - (typ == TYPE_ENUM.T_UINT4) || - (typ == TYPE_ENUM.T_UINT8) || - (typ == TYPE_ENUM.T_UINT16)); - } - - internal static bool CV_TYP_IS_REAL(TYPE_ENUM typ) { - return ((CV_TYPE(typ) == CV_type.CV_REAL) && CV_TYP_IS_DIRECT(typ)); - } + //internal static bool CV_TYP_IS_DIRECT(TYPE_ENUM typ) { + // return (CV_MODE(typ) == CV_prmode.CV_TM_DIRECT); + //} + + //internal static bool CV_TYP_IS_PTR(TYPE_ENUM typ) { + // return (CV_MODE(typ) != CV_prmode.CV_TM_DIRECT); + //} + + //internal static bool CV_TYP_IS_SIGNED(TYPE_ENUM typ) { + // return + // (((CV_TYPE(typ) == CV_type.CV_SIGNED) && CV_TYP_IS_DIRECT(typ)) || + // (typ == TYPE_ENUM.T_INT1) || + // (typ == TYPE_ENUM.T_INT2) || + // (typ == TYPE_ENUM.T_INT4) || + // (typ == TYPE_ENUM.T_INT8) || + // (typ == TYPE_ENUM.T_INT16) || + // (typ == TYPE_ENUM.T_RCHAR)); + //} + + //internal static bool CV_TYP_IS_UNSIGNED(TYPE_ENUM typ) { + // return (((CV_TYPE(typ) == CV_type.CV_UNSIGNED) && CV_TYP_IS_DIRECT(typ)) || + // (typ == TYPE_ENUM.T_UINT1) || + // (typ == TYPE_ENUM.T_UINT2) || + // (typ == TYPE_ENUM.T_UINT4) || + // (typ == TYPE_ENUM.T_UINT8) || + // (typ == TYPE_ENUM.T_UINT16)); + //} + + //internal static bool CV_TYP_IS_REAL(TYPE_ENUM typ) { + // return ((CV_TYPE(typ) == CV_type.CV_REAL) && CV_TYP_IS_DIRECT(typ)); + //} const uint CV_FIRST_NONPRIM = 0x1000; - internal static bool CV_IS_PRIMITIVE(TYPE_ENUM typ) { - return ((uint)(typ) < CV_FIRST_NONPRIM); - } + //internal static bool CV_IS_PRIMITIVE(TYPE_ENUM typ) { + // return ((uint)(typ) < CV_FIRST_NONPRIM); + //} - internal static bool CV_TYP_IS_COMPLEX(TYPE_ENUM typ) { - return ((CV_TYPE(typ) == CV_type.CV_COMPLEX) && CV_TYP_IS_DIRECT(typ)); - } + //internal static bool CV_TYP_IS_COMPLEX(TYPE_ENUM typ) { + // return ((CV_TYPE(typ) == CV_type.CV_COMPLEX) && CV_TYP_IS_DIRECT(typ)); + //} - internal static bool CV_IS_INTERNAL_PTR(TYPE_ENUM typ) { - return (CV_IS_PRIMITIVE(typ) && - CV_TYPE(typ) == CV_type.CV_CVRESERVED && - CV_TYP_IS_PTR(typ)); - } + //internal static bool CV_IS_INTERNAL_PTR(TYPE_ENUM typ) { + // return (CV_IS_PRIMITIVE(typ) && + // CV_TYPE(typ) == CV_type.CV_CVRESERVED && + // CV_TYP_IS_PTR(typ)); + //} } // selected values for type_index - for a more complete definition, see diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DataStream.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DataStream.cs index 99929d980..48a185165 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DataStream.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DataStream.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -9,8 +14,6 @@ using System.IO; namespace Microsoft.Cci.Pdb { internal class DataStream { internal DataStream() { - this.contentSize = 0; - this.pages = null; } internal DataStream(int contentSize, BitAccess bits, int count) { @@ -72,73 +75,35 @@ namespace Microsoft.Cci.Pdb { } } - internal void Write(PdbWriter writer, byte[] bytes) { - Write(writer, bytes, bytes.Length); - } - - internal void Write(PdbWriter writer, byte[] bytes, int data) { - if (bytes == null || data == 0) { - return; - } - - int left = data; - int used = 0; - int rema = contentSize % writer.pageSize; - if (rema != 0) { - int todo = writer.pageSize - rema; - if (todo > left) { - todo = left; - } - - int lastPage = pages[pages.Length - 1]; - writer.Seek(lastPage, rema); - writer.Write(bytes, used, todo); - used += todo; - left -= todo; - } - - if (left > 0) { - int count = (left + writer.pageSize - 1) / writer.pageSize; - int page0 = writer.AllocatePages(count); - - writer.Seek(page0, 0); - writer.Write(bytes, used, left); - - AddPages(page0, count); - } - - contentSize += data; - } - - private void AddPages(int page0, int count) { - if (pages == null) { - pages = new int[count]; - for (int i = 0; i < count; i++) { - pages[i] = page0 + i; - } - } else { - int[] old = pages; - int used = old.Length; - - pages = new int[used + count]; - Array.Copy(old, pages, used); - for (int i = 0; i < count; i++) { - pages[used + i] = page0 + i; - } - } - } - - internal int Pages { - get { return pages == null ? 0 : pages.Length; } - } + //private void AddPages(int page0, int count) { + // if (pages == null) { + // pages = new int[count]; + // for (int i = 0; i < count; i++) { + // pages[i] = page0 + i; + // } + // } else { + // int[] old = pages; + // int used = old.Length; + + // pages = new int[used + count]; + // Array.Copy(old, pages, used); + // for (int i = 0; i < count; i++) { + // pages[used + i] = page0 + i; + // } + // } + //} + + //internal int Pages { + // get { return pages == null ? 0 : pages.Length; } + //} internal int Length { get { return contentSize; } } - internal int GetPage(int index) { - return pages[index]; - } + //internal int GetPage(int index) { + // return pages[index]; + //} internal int contentSize; internal int[] pages; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiDbgHdr.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiDbgHdr.cs index b6159ab7a..588f3c13d 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiDbgHdr.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiDbgHdr.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiHeader.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiHeader.cs index 29fb9fd9f..0ca791588 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiHeader.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiHeader.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiModuleInfo.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiModuleInfo.cs index a9a5b2ab2..8ab371711 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiModuleInfo.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiModuleInfo.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -9,7 +14,7 @@ namespace Microsoft.Cci.Pdb { internal class DbiModuleInfo { internal DbiModuleInfo(BitAccess bits, bool readStrings) { bits.ReadInt32(out opened); - section = new DbiSecCon(bits); + new DbiSecCon(bits); bits.ReadUInt16(out flags); bits.ReadInt16(out stream); bits.ReadInt32(out cbSyms); @@ -35,7 +40,7 @@ namespace Microsoft.Cci.Pdb { } internal int opened; // 0..3 - internal DbiSecCon section; // 4..31 + //internal DbiSecCon section; // 4..31 internal ushort flags; // 32..33 internal short stream; // 34..35 internal int cbSyms; // 36..39 diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiSecCon.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiSecCon.cs index 1e2b60d78..de9fde951 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiSecCon.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/DbiSecCon.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/IntHashTable.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/IntHashTable.cs index a092b1ca0..db0e41be0 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/IntHashTable.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/IntHashTable.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -54,7 +59,7 @@ namespace Microsoft.Cci.Pdb { // By Brian Grunkemeyer, algorithm by Patrick Dussud. // Version 1.30 2/20/2000 //| - internal class IntHashTable : IEnumerable { + internal class IntHashTable {//: IEnumerable { /* Implementation Notes: @@ -125,7 +130,7 @@ namespace Microsoft.Cci.Pdb { 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369}; - private int GetPrime(int minSize) { + private static int GetPrime(int minSize) { if (minSize < 0) { throw new ArgumentException("Arg_HTCapacityOverflow"); } @@ -168,17 +173,17 @@ namespace Microsoft.Cci.Pdb { : this(0, 100) { } - // Constructs a new hashtable with the given initial capacity and a load - // factor of 1.0. The capacity argument serves as an indication of - // the number of entries the hashtable will contain. When this number (or - // an approximation) is known, specifying it in the constructor can - // eliminate a number of resizing operations that would otherwise be - // performed when elements are added to the hashtable. - // - //| - internal IntHashTable(int capacity) - : this(capacity, 100) { - } + //// Constructs a new hashtable with the given initial capacity and a load + //// factor of 1.0. The capacity argument serves as an indication of + //// the number of entries the hashtable will contain. When this number (or + //// an approximation) is known, specifying it in the constructor can + //// eliminate a number of resizing operations that would otherwise be + //// performed when elements are added to the hashtable. + //// + ////| + //internal IntHashTable(int capacity) + // : this(capacity, 100) { + //} // Constructs a new hashtable with the given initial capacity and load // factor. The capacity argument serves as an indication of the @@ -213,7 +218,7 @@ namespace Microsoft.Cci.Pdb { // The out parameter seed is h1(key), while the out parameter // incr is h2(key, hashSize). Callers of this function should // add incr each time through a loop. - private uint InitHash(int key, int hashsize, out uint seed, out uint incr) { + private static uint InitHash(int key, int hashsize, out uint seed, out uint incr) { // Hashcode must be positive. Also, we must not use the sign bit, since // that is used for the collision bit. uint hashcode = (uint)key & 0x7FFFFFFF; @@ -236,52 +241,52 @@ namespace Microsoft.Cci.Pdb { Insert(key, value, true); } - // Removes all entries from this hashtable. - //| - internal void Clear() { - if (count == 0) - return; + //// Removes all entries from this hashtable. + ////| + //internal void Clear() { + // if (count == 0) + // return; - for (int i = 0; i < buckets.Length; i++) { - buckets[i].hash_coll = 0; - buckets[i].key = -1; - buckets[i].val = null; - } + // for (int i = 0; i < buckets.Length; i++) { + // buckets[i].hash_coll = 0; + // buckets[i].key = -1; + // buckets[i].val = null; + // } - count = 0; - occupancy = 0; - } + // count = 0; + // occupancy = 0; + //} // Checks if this hashtable contains an entry with the given key. This is // an O(1) operation. // //| - internal bool Contains(int key) { - if (key < 0) { - throw new ArgumentException("Argument_KeyLessThanZero"); - } - - uint seed; - uint incr; - // Take a snapshot of buckets, in case another thread resizes table - bucket[] lbuckets = buckets; - uint hashcode = InitHash(key, lbuckets.Length, out seed, out incr); - int ntry = 0; - - bucket b; - do { - int bucketNumber = (int)(seed % (uint)lbuckets.Length); - b = lbuckets[bucketNumber]; - if (b.val == null) { - return false; - } - if (((b.hash_coll & 0x7FFFFFFF) == hashcode) && b.key == key) { - return true; - } - seed += incr; - } while (b.hash_coll < 0 && ++ntry < lbuckets.Length); - return false; - } + //internal bool Contains(int key) { + // if (key < 0) { + // throw new ArgumentException("Argument_KeyLessThanZero"); + // } + + // uint seed; + // uint incr; + // // Take a snapshot of buckets, in case another thread resizes table + // bucket[] lbuckets = buckets; + // uint hashcode = InitHash(key, lbuckets.Length, out seed, out incr); + // int ntry = 0; + + // bucket b; + // do { + // int bucketNumber = (int)(seed % (uint)lbuckets.Length); + // b = lbuckets[bucketNumber]; + // if (b.val == null) { + // return false; + // } + // if (((b.hash_coll & 0x7FFFFFFF) == hashcode) && b.key == key) { + // return true; + // } + // seed += incr; + // } while (b.hash_coll < 0 && ++ntry < lbuckets.Length); + // return false; + //} // Returns the value associated with the given key. If an entry with the // given key is not found, the returned value is null. @@ -313,9 +318,9 @@ namespace Microsoft.Cci.Pdb { } while (b.hash_coll < 0 && ++ntry < lbuckets.Length); return null; } - set { - Insert(key, value, false); - } + //set { + // Insert(key, value, false); + //} } // Increases the bucket count of this hashtable. This method is called from @@ -374,9 +379,9 @@ namespace Microsoft.Cci.Pdb { // enumerator will throw an exception. // //| - IEnumerator IEnumerable.GetEnumerator() { - return new IntHashTableEnumerator(this); - } + //IEnumerator IEnumerable.GetEnumerator() { + // return new IntHashTableEnumerator(this); + //} // Internal method to compare two keys. // @@ -502,78 +507,77 @@ namespace Microsoft.Cci.Pdb { // Returns the number of associations in this hashtable. // //| - internal int Count { - get { return count; } - } + //internal int Count { + // get { return count; } + //} // Implements an enumerator for a hashtable. The enumerator uses the // internal version number of the hashtabke to ensure that no modifications // are made to the hashtable while an enumeration is in progress. - private class IntHashTableEnumerator : IEnumerator { - private IntHashTable hashtable; - private int bucket; - private int version; - private bool current; - private int currentKey; - private Object currentValue; - - internal IntHashTableEnumerator(IntHashTable hashtable) { - this.hashtable = hashtable; - bucket = hashtable.buckets.Length; - version = hashtable.version; - current = false; - } - - public bool MoveNext() { - if (version != hashtable.version) - throw new InvalidOperationException("InvalidOperation_EnumFailedVersion"); - while (bucket > 0) { - bucket--; - Object val = hashtable.buckets[bucket].val; - if (val != null) { - currentKey = hashtable.buckets[bucket].key; - currentValue = val; - current = true; - return true; - } - } - current = false; - return false; - } - - internal int Key { - get { - if (current == false) - throw new InvalidOperationException("InvalidOperation_EnumOpCantHappen"); - return currentKey; - } - } - - public Object Current { - get { - if (current == false) - throw new InvalidOperationException("InvalidOperation_EnumOpCantHappen"); - return currentValue; - } - } - - public Object Value { - get { - if (version != hashtable.version) - throw new InvalidOperationException("InvalidOperation_EnumFailedVersion"); - if (current == false) - throw new InvalidOperationException("InvalidOperation_EnumOpCantHappen"); - return currentValue; - } - } - - public void Reset() { - if (version != hashtable.version) throw new InvalidOperationException("InvalidOperation_EnumFailedVersion"); - current = false; - bucket = hashtable.buckets.Length; - currentKey = -1; - currentValue = null; - } - } + //private class IntHashTableEnumerator : IEnumerator { + // private IntHashTable hashtable; + // private int bucket; + // private int version; + // private bool current; + // //private int currentKey; + // private Object currentValue; + + // internal IntHashTableEnumerator(IntHashTable hashtable) { + // this.hashtable = hashtable; + // bucket = hashtable.buckets.Length; + // version = hashtable.version; + // } + + // public bool MoveNext() { + // if (version != hashtable.version) + // throw new InvalidOperationException("InvalidOperation_EnumFailedVersion"); + // while (bucket > 0) { + // bucket--; + // Object val = hashtable.buckets[bucket].val; + // if (val != null) { + // //currentKey = hashtable.buckets[bucket].key; + // currentValue = val; + // current = true; + // return true; + // } + // } + // current = false; + // return false; + // } + + // //internal int Key { + // // get { + // // if (current == false) + // // throw new InvalidOperationException("InvalidOperation_EnumOpCantHappen"); + // // return currentKey; + // // } + // //} + + // public Object Current { + // get { + // if (current == false) + // throw new InvalidOperationException("InvalidOperation_EnumOpCantHappen"); + // return currentValue; + // } + // } + + // //public Object Value { + // // get { + // // if (version != hashtable.version) + // // throw new InvalidOperationException("InvalidOperation_EnumFailedVersion"); + // // if (current == false) + // // throw new InvalidOperationException("InvalidOperation_EnumOpCantHappen"); + // // return currentValue; + // // } + // //} + + // public void Reset() { + // if (version != hashtable.version) throw new InvalidOperationException("InvalidOperation_EnumFailedVersion"); + // current = false; + // bucket = hashtable.buckets.Length; + // //currentKey = -1; + // currentValue = null; + // } + //} } } diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/MsfDirectory.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/MsfDirectory.cs index 3a7910d7d..a6669b5bb 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/MsfDirectory.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/MsfDirectory.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -8,12 +13,20 @@ using System; namespace Microsoft.Cci.Pdb { internal class MsfDirectory { internal MsfDirectory(PdbReader reader, PdbFileHeader head, BitAccess bits) { - bits.MinCapacity(head.directorySize); int pages = reader.PagesFromSize(head.directorySize); // 0..n in page of directory pages. - reader.Seek(head.directoryRoot, 0); - bits.FillBuffer(reader.reader, pages * 4); + bits.MinCapacity(head.directorySize); + int directoryRootPages = head.directoryRoot.Length; + int pagesPerPage = head.pageSize / 4; + int pagesToGo = pages; + for (int i = 0; i < directoryRootPages; i++) { + int pagesInThisPage = pagesToGo <= pagesPerPage ? pagesToGo : pagesPerPage; + reader.Seek(head.directoryRoot[i], 0); + bits.Append(reader.reader, pagesInThisPage * 4); + pagesToGo -= pagesInThisPage; + } + bits.Position = 0; DataStream stream = new DataStream(head.directorySize, bits, pages); bits.MinCapacity(head.directorySize); diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbConstant.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbConstant.cs index 1f1aec1fd..434841b0c 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbConstant.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbConstant.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -22,6 +27,11 @@ namespace Microsoft.Cci.Pdb { this.value = tag1; } else if (tag2 == 0x80) { switch (tag1) { + case 0x00: //sbyte + sbyte sb; + bits.ReadInt8(out sb); + this.value = sb; + break; case 0x01: //short short s; bits.ReadInt16(out s); diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbDebugException.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbDebugException.cs index 515dc37c7..d7f8f0fdb 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbDebugException.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbDebugException.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbException.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbException.cs index 0dd7f9384..38d1d5636 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbException.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbException.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFile.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFile.cs index d6d493ccf..9e5602812 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFile.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFile.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -21,7 +26,7 @@ namespace Microsoft.Cci.Pdb { bits.ReadGuid(out doctype); } - static Dictionary LoadNameIndex(BitAccess bits, out int age, out Guid guid) { + static Dictionary LoadNameIndex(BitAccess bits, out int age, out Guid guid) { Dictionary result = new Dictionary(); int ver; int sig; @@ -30,9 +35,9 @@ namespace Microsoft.Cci.Pdb { bits.ReadInt32(out age); // 8..11 Age bits.ReadGuid(out guid); // 12..27 GUID - if (ver != 20000404) { - throw new PdbDebugException("Unsupported PDB Stream version {0}", ver); - } + //if (ver != 20000404) { + // throw new PdbDebugException("Unsupported PDB Stream version {0}", ver); + //} // Read string buffer. int buf; @@ -70,7 +75,7 @@ namespace Microsoft.Cci.Pdb { bits.ReadCString(out name); bits.Position = saved; - result.Add(name, ni); + result.Add(name.ToUpperInvariant(), ni); j++; } } @@ -128,15 +133,11 @@ namespace Microsoft.Cci.Pdb { private static PdbFunction match = new PdbFunction(); - private static PdbFunction FindFunction(PdbFunction[] funcs, ushort sec, uint off) { + private static int FindFunction(PdbFunction[] funcs, ushort sec, uint off) { match.segment = sec; match.address = off; - int item = Array.BinarySearch(funcs, match, PdbFunction.byAddress); - if (item >= 0) { - return funcs[item]; - } - return null; + return Array.BinarySearch(funcs, match, PdbFunction.byAddress); } static void LoadManagedLines(PdbFunction[] funcs, @@ -146,7 +147,7 @@ namespace Microsoft.Cci.Pdb { Dictionary nameIndex, PdbReader reader, uint limit) { - Array.Sort(funcs, PdbFunction.byAddress); + Array.Sort(funcs, PdbFunction.byAddressAndToken); IntHashTable checks = new IntHashTable(); // Read the files first @@ -172,15 +173,15 @@ namespace Microsoft.Cci.Pdb { string name = (string)names[(int)chk.name]; int guidStream; Guid doctypeGuid = SymDocumentType.Text; - Guid languageGuid = SymLanguageType.CSharp; - Guid vendorGuid = SymLanguageVendor.Microsoft; - if (nameIndex.TryGetValue("/src/files/"+name, out guidStream)) { + Guid languageGuid = Guid.Empty; + Guid vendorGuid = Guid.Empty; + if (nameIndex.TryGetValue("/SRC/FILES/"+name.ToUpperInvariant(), out guidStream)) { var guidBits = new BitAccess(0x100); dir.streams[guidStream].Read(reader, guidBits); LoadGuidStream(guidBits, out doctypeGuid, out languageGuid, out vendorGuid); } - PdbSource src = new PdbSource((uint)ni, name, doctypeGuid, languageGuid, vendorGuid); + PdbSource src = new PdbSource(/*(uint)ni,*/ name, doctypeGuid, languageGuid, vendorGuid); checks.Add(ni, src); bits.Position += chk.len; bits.Align(4); @@ -211,8 +212,25 @@ namespace Microsoft.Cci.Pdb { bits.ReadUInt16(out sec.sec); bits.ReadUInt16(out sec.flags); bits.ReadUInt32(out sec.cod); - PdbFunction func = FindFunction(funcs, sec.sec, sec.off); - if (func == null) break; + int funcIndex = FindFunction(funcs, sec.sec, sec.off); + if (funcIndex < 0) break; + var func = funcs[funcIndex]; + if (func.lines == null) { + while (funcIndex > 0) { + var f = funcs[funcIndex-1]; + if (f.lines != null || f.segment != sec.sec || f.address != sec.off) break; + func = f; + funcIndex--; + } + } else { + while (funcIndex < funcs.Length-1 && func.lines != null) { + var f = funcs[funcIndex+1]; + if (f.segment != sec.sec || f.address != sec.off) break; + func = f; + funcIndex++; + } + } + if (func.lines != null) break; // Count the line blocks. int begSym = bits.Position; @@ -255,7 +273,7 @@ namespace Microsoft.Cci.Pdb { uint lineBegin = line.flags & (uint)CV_Line_Flags.linenumStart; uint delta = (line.flags & (uint)CV_Line_Flags.deltaLineEnd) >> 24; - bool statement = ((line.flags & (uint)CV_Line_Flags.fStatement) == 0); + //bool statement = ((line.flags & (uint)CV_Line_Flags.fStatement) == 0); if ((sec.flags & 1) != 0) { bits.Position = pcol + 4 * i; bits.ReadUInt16(out column.offColumnStart); @@ -295,7 +313,7 @@ namespace Microsoft.Cci.Pdb { bits.Position = 4; // Console.WriteLine("{0}:", info.moduleName); - funcs = PdbFunction.LoadManagedFunctions(info.moduleName, + funcs = PdbFunction.LoadManagedFunctions(/*info.moduleName,*/ bits, (uint)info.cbSyms, readStrings); if (funcs != null) { @@ -316,10 +334,10 @@ namespace Microsoft.Cci.Pdb { DbiHeader dh = new DbiHeader(bits); header = new DbiDbgHdr(); - if (dh.sig != -1 || dh.ver != 19990903) { - throw new PdbException("Unsupported DBI Stream version, sig={0}, ver={1}", - dh.sig, dh.ver); - } + //if (dh.sig != -1 || dh.ver != 19990903) { + // throw new PdbException("Unsupported DBI Stream version, sig={0}, ver={1}", + // dh.sig, dh.ver); + //} // Read gpmod section. ArrayList modList = new ArrayList(); @@ -377,7 +395,7 @@ namespace Microsoft.Cci.Pdb { dir.streams[1].Read(reader, bits); Dictionary nameIndex = LoadNameIndex(bits, out age, out guid); int nameStream; - if (!nameIndex.TryGetValue("/names", out nameStream)) { + if (!nameIndex.TryGetValue("/NAMES", out nameStream)) { throw new PdbException("No `name' stream"); } @@ -413,7 +431,7 @@ namespace Microsoft.Cci.Pdb { } // - Array.Sort(funcs, PdbFunction.byAddress); + Array.Sort(funcs, PdbFunction.byAddressAndToken); //Array.Sort(funcs, PdbFunction.byToken); return funcs; } diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFileHeader.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFileHeader.cs index c35107650..e1f56dbe4 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFileHeader.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFileHeader.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -9,21 +14,20 @@ using System.Text; namespace Microsoft.Cci.Pdb { internal class PdbFileHeader { - internal PdbFileHeader(int pageSize) { - this.magic = new byte[32] { - 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, // "Microsof" - 0x74, 0x20, 0x43, 0x2F, 0x43, 0x2B, 0x2B, 0x20, // "t C/C++ " - 0x4D, 0x53, 0x46, 0x20, 0x37, 0x2E, 0x30, 0x30, // "MSF 7.00" - 0x0D, 0x0A, 0x1A, 0x44, 0x53, 0x00, 0x00, 0x00 // "^^^DS^^^" - }; - this.pageSize = pageSize; - this.zero = 0; - } + //internal PdbFileHeader(int pageSize) { + // this.magic = new byte[32] { + // 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, // "Microsof" + // 0x74, 0x20, 0x43, 0x2F, 0x43, 0x2B, 0x2B, 0x20, // "t C/C++ " + // 0x4D, 0x53, 0x46, 0x20, 0x37, 0x2E, 0x30, 0x30, // "MSF 7.00" + // 0x0D, 0x0A, 0x1A, 0x44, 0x53, 0x00, 0x00, 0x00 // "^^^DS^^^" + // }; + // this.pageSize = pageSize; + //} internal PdbFileHeader(Stream reader, BitAccess bits) { bits.MinCapacity(56); reader.Seek(0, SeekOrigin.Begin); - bits.FillBuffer(reader, 56); + bits.FillBuffer(reader, 52); this.magic = new byte[32]; bits.ReadBytes(this.magic); // 0..31 @@ -32,41 +36,45 @@ namespace Microsoft.Cci.Pdb { bits.ReadInt32(out this.pagesUsed); // 40..43 bits.ReadInt32(out this.directorySize); // 44..47 bits.ReadInt32(out this.zero); // 48..51 - bits.ReadInt32(out this.directoryRoot); // 52..55 - } - internal string Magic { - get { return StringFromBytesUTF8(magic); } + int directoryPages = ((((directorySize + pageSize - 1) / pageSize) * 4) + pageSize - 1) / pageSize; + this.directoryRoot = new int[directoryPages]; + bits.FillBuffer(reader, directoryPages * 4); + bits.ReadInt32(this.directoryRoot); } - internal void Write(Stream writer, BitAccess bits) { - bits.MinCapacity(56); - bits.WriteBytes(magic); // 0..31 - bits.WriteInt32(pageSize); // 32..35 - bits.WriteInt32(freePageMap); // 36..39 - bits.WriteInt32(pagesUsed); // 40..43 - bits.WriteInt32(directorySize); // 44..47 - bits.WriteInt32(zero); // 48..51 - bits.WriteInt32(directoryRoot); // 52..55 + //internal string Magic { + // get { return StringFromBytesUTF8(magic); } + //} - writer.Seek(0, SeekOrigin.Begin); - bits.WriteBuffer(writer, 56); - } + //internal void Write(Stream writer, BitAccess bits) { + // bits.MinCapacity(pageSize); + // bits.WriteBytes(magic); // 0..31 + // bits.WriteInt32(pageSize); // 32..35 + // bits.WriteInt32(freePageMap); // 36..39 + // bits.WriteInt32(pagesUsed); // 40..43 + // bits.WriteInt32(directorySize); // 44..47 + // bits.WriteInt32(zero); // 48..51 + // bits.WriteInt32(directoryRoot); // 52..55 + + // writer.Seek(0, SeekOrigin.Begin); + // bits.WriteBuffer(writer, pageSize); + //} //////////////////////////////////////////////////// Helper Functions. // - internal string StringFromBytesUTF8(byte[] bytes) { - return StringFromBytesUTF8(bytes, 0, bytes.Length); - } + //internal static string StringFromBytesUTF8(byte[] bytes) { + // return StringFromBytesUTF8(bytes, 0, bytes.Length); + //} - internal string StringFromBytesUTF8(byte[] bytes, int offset, int length) { - for (int i = 0; i < length; i++) { - if (bytes[offset + i] < ' ') { - length = i; - } - } - return Encoding.UTF8.GetString(bytes, offset, length); - } + //internal static string StringFromBytesUTF8(byte[] bytes, int offset, int length) { + // for (int i = 0; i < length; i++) { + // if (bytes[offset + i] < ' ') { + // length = i; + // } + // } + // return Encoding.UTF8.GetString(bytes, offset, length); + //} ////////////////////////////////////////////////////////////// Fields. // @@ -76,7 +84,7 @@ namespace Microsoft.Cci.Pdb { internal int pagesUsed; internal int directorySize; internal readonly int zero; - internal int directoryRoot; + internal int[] directoryRoot; } } diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFunction.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFunction.cs index be284204d..d1daba14e 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFunction.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFunction.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -12,20 +17,24 @@ namespace Microsoft.Cci.Pdb { static internal readonly Guid msilMetaData = new Guid(0xc6ea3fc9, 0x59b3, 0x49d6, 0xbc, 0x25, 0x09, 0x02, 0xbb, 0xab, 0xb4, 0x60); static internal readonly IComparer byAddress = new PdbFunctionsByAddress(); - static internal readonly IComparer byToken = new PdbFunctionsByToken(); + static internal readonly IComparer byAddressAndToken = new PdbFunctionsByAddressAndToken(); + //static internal readonly IComparer byToken = new PdbFunctionsByToken(); internal uint token; internal uint slotToken; - internal string name; - internal string module; - internal ushort flags; + //internal string name; + //internal string module; + //internal ushort flags; internal uint segment; internal uint address; - internal uint length; + //internal uint length; //internal byte[] metadata; internal PdbScope[] scopes; + internal PdbSlot[] slots; + internal PdbConstant[] constants; + internal string[] usedNamespaces; internal PdbLines[] lines; internal ushort[]/*?*/ usingCounts; internal IEnumerable/*?*/ namespaceScopes; @@ -41,10 +50,10 @@ namespace Microsoft.Cci.Pdb { } - internal static PdbFunction[] LoadManagedFunctions(string module, + internal static PdbFunction[] LoadManagedFunctions(/*string module,*/ BitAccess bits, uint limit, bool readStrings) { - string mod = StripNamespace(module); + //string mod = StripNamespace(module); int begin = bits.Position; int count = 0; @@ -101,7 +110,7 @@ namespace Microsoft.Cci.Pdb { case SYM.S_GMANPROC: case SYM.S_LMANPROC: ManProcSym proc; - int offset = bits.Position; + //int offset = bits.Position; bits.ReadUInt32(out proc.parent); bits.ReadUInt32(out proc.end); @@ -122,7 +131,7 @@ namespace Microsoft.Cci.Pdb { //Console.WriteLine("token={0:X8} [{1}::{2}]", proc.token, module, proc.name); bits.Position = stop; - funcs[func++] = new PdbFunction(module, proc, bits); + funcs[func++] = new PdbFunction(/*module,*/ proc, bits); break; default: { @@ -190,15 +199,14 @@ namespace Microsoft.Cci.Pdb { internal PdbFunction() { } - internal PdbFunction(string module, ManProcSym proc, BitAccess bits) { + internal PdbFunction(/*string module, */ManProcSym proc, BitAccess bits) { this.token = proc.token; - this.module = module; - this.name = proc.name; - this.flags = proc.flags; + //this.module = module; + //this.name = proc.name; + //this.flags = proc.flags; this.segment = proc.seg; this.address = proc.off; - this.length = proc.len; - this.slotToken = 0; + //this.length = proc.len; if (proc.seg != 1) { throw new PdbDebugException("Segment is {0}, not 1.", proc.seg); @@ -207,18 +215,27 @@ namespace Microsoft.Cci.Pdb { throw new PdbDebugException("Warning parent={0}, next={1}", proc.parent, proc.next); } - if (proc.dbgStart != 0 || proc.dbgEnd != 0) { - throw new PdbDebugException("Warning DBG start={0}, end={1}", - proc.dbgStart, proc.dbgEnd); - } + //if (proc.dbgStart != 0 || proc.dbgEnd != 0) { + // throw new PdbDebugException("Warning DBG start={0}, end={1}", + // proc.dbgStart, proc.dbgEnd); + //} int constantCount; int scopeCount; int slotCount; int usedNamespacesCount; CountScopesAndSlots(bits, proc.end, out constantCount, out scopeCount, out slotCount, out usedNamespacesCount); - scopes = new PdbScope[scopeCount]; - int scope = 0; + int scope = constantCount > 0 || slotCount > 0 || usedNamespacesCount > 0 ? 1 : 0; + int slot = 0; + int constant = 0; + int usedNs = 0; + scopes = new PdbScope[scopeCount+scope]; + slots = new PdbSlot[slotCount]; + constants = new PdbConstant[constantCount]; + usedNamespaces = new string[usedNamespacesCount]; + + if (scope > 0) + scopes[0] = new PdbScope(this.address, proc.len, slots, constants, usedNamespaces); while (bits.Position < proc.end) { ushort siz; @@ -266,17 +283,29 @@ namespace Microsoft.Cci.Pdb { bits.ReadUInt32(out block.parent); bits.ReadUInt32(out block.end); bits.ReadUInt32(out block.len); - bits.ReadUInt32(out this.address); + bits.ReadUInt32(out block.off); bits.ReadUInt16(out block.seg); bits.SkipCString(out block.name); bits.Position = stop; - scopes[scope] = new PdbScope(block, bits, out slotToken); + scopes[scope++] = new PdbScope(this.address, block, bits, out slotToken); bits.Position = (int)block.end; break; } + case SYM.S_MANSLOT: + uint typind; + slots[slot++] = new PdbSlot(bits, out typind); + bits.Position = stop; + break; + + case SYM.S_MANCONSTANT: + constants[constant++] = new PdbConstant(bits); + bits.Position = stop; + break; + case SYM.S_UNAMESPACE: + bits.ReadCString(out usedNamespaces[usedNs++]); bits.Position = stop; break; @@ -320,8 +349,8 @@ namespace Microsoft.Cci.Pdb { bits.ReadUInt32(out numberOfBytesInItem); switch (kind) { case 0: this.ReadUsingInfo(bits); break; - case 1: this.ReadForwardInfo(bits); break; - case 2: this.ReadForwardedToModuleInfo(bits); break; + case 1: break; // this.ReadForwardInfo(bits); break; + case 2: break; // this.ReadForwardedToModuleInfo(bits); break; case 3: this.ReadIteratorLocals(bits); break; case 4: this.ReadForwardIterator(bits); break; default: throw new PdbDebugException("Unknown custom metadata item kind: {0}", kind); @@ -346,11 +375,11 @@ namespace Microsoft.Cci.Pdb { } } - private void ReadForwardedToModuleInfo(BitAccess bits) { - } + //private void ReadForwardedToModuleInfo(BitAccess bits) { + //} - private void ReadForwardInfo(BitAccess bits) { - } + //private void ReadForwardInfo(BitAccess bits) { + //} private void ReadUsingInfo(BitAccess bits) { ushort numberOfNamespaces; @@ -380,20 +409,44 @@ namespace Microsoft.Cci.Pdb { } } - internal class PdbFunctionsByToken : IComparer { + internal class PdbFunctionsByAddressAndToken : IComparer { public int Compare(Object x, Object y) { PdbFunction fx = (PdbFunction)x; PdbFunction fy = (PdbFunction)y; - if (fx.token < fy.token) { + if (fx.segment < fy.segment) { + return -1; + } else if (fx.segment > fy.segment) { + return 1; + } else if (fx.address < fy.address) { return -1; - } else if (fx.token > fy.token) { + } else if (fx.address > fy.address) { return 1; } else { - return 0; + if (fx.token < fy.token) + return -1; + else if (fx.token > fy.token) + return 1; + else + return 0; } } - } + + //internal class PdbFunctionsByToken : IComparer { + // public int Compare(Object x, Object y) { + // PdbFunction fx = (PdbFunction)x; + // PdbFunction fy = (PdbFunction)y; + + // if (fx.token < fy.token) { + // return -1; + // } else if (fx.token > fy.token) { + // return 1; + // } else { + // return 0; + // } + // } + + //} } } diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLine.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLine.cs index 78eb9d630..f6fe3a9d3 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLine.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLine.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLines.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLines.cs index 9e989cdf3..382638b70 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLines.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbLines.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbReader.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbReader.cs index 52a8f2a2f..edfd9263c 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbReader.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbReader.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -25,9 +30,9 @@ namespace Microsoft.Cci.Pdb { return (size + pageSize - 1) / (pageSize); } - internal int PageSize { - get { return pageSize; } - } + //internal int PageSize { + // get { return pageSize; } + //} internal readonly int pageSize; internal readonly Stream reader; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbScope.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbScope.cs index 72a68d780..c46220b80 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbScope.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbScope.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -12,13 +17,25 @@ namespace Microsoft.Cci.Pdb { internal PdbScope[] scopes; internal string[] usedNamespaces; - internal uint segment; + //internal uint segment; internal uint address; + internal uint offset; internal uint length; - internal PdbScope(BlockSym32 block, BitAccess bits, out uint typind) { - this.segment = block.seg; + internal PdbScope(uint address, uint length, PdbSlot[] slots, PdbConstant[] constants, string[] usedNamespaces) { + this.constants = constants; + this.slots = slots; + this.scopes = new PdbScope[0]; + this.usedNamespaces = usedNamespaces; + this.address = address; + this.offset = 0; + this.length = length; + } + + internal PdbScope(uint funcOffset, BlockSym32 block, BitAccess bits, out uint typind) { + //this.segment = block.seg; this.address = block.off; + this.offset = block.off - funcOffset; this.length = block.len; typind = 0; @@ -58,7 +75,7 @@ namespace Microsoft.Cci.Pdb { bits.SkipCString(out sub.name); bits.Position = stop; - scopes[scope++] = new PdbScope(sub, bits, out typind); + scopes[scope++] = new PdbScope(funcOffset, sub, bits, out typind); break; } @@ -82,8 +99,9 @@ namespace Microsoft.Cci.Pdb { break; default: - throw new PdbException("Unknown SYM in scope {0}", (SYM)rec); - // bits.Position = stop; + //throw new PdbException("Unknown SYM in scope {0}", (SYM)rec); + bits.Position = stop; + break; } } diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSlot.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSlot.cs index b2cebbeb9..0dc89ad4d 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSlot.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSlot.cs @@ -1,6 +1,11 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; @@ -10,8 +15,8 @@ namespace Microsoft.Cci.Pdb { internal uint slot; internal string name; internal ushort flags; - internal uint segment; - internal uint address; + //internal uint segment; + //internal uint address; internal PdbSlot(BitAccess bits, out uint typind) { AttrSlotSym slot; @@ -26,8 +31,8 @@ namespace Microsoft.Cci.Pdb { this.slot = slot.index; this.name = slot.name; this.flags = slot.flags; - this.segment = slot.segCod; - this.address = slot.offCod; + //this.segment = slot.segCod; + //this.address = slot.offCod; typind = slot.typind; } diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSource.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSource.cs index e7b0e3402..ac40f8516 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSource.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbSource.cs @@ -1,20 +1,25 @@ //----------------------------------------------------------------------------- // -// Copyright (C) Microsoft Corporation. All Rights Reserved. +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the Microsoft Public License. +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //----------------------------------------------------------------------------- using System; namespace Microsoft.Cci.Pdb { internal class PdbSource { - internal uint index; + //internal uint index; internal string name; internal Guid doctype; internal Guid language; internal Guid vendor; - internal PdbSource(uint index, string name, Guid doctype, Guid language, Guid vendor) { - this.index = index; + internal PdbSource(/*uint index, */string name, Guid doctype, Guid language, Guid vendor) { + //this.index = index; this.name = name; this.doctype = doctype; this.language = language; diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbWriter.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbWriter.cs deleted file mode 100644 index ca4992eed..000000000 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbWriter.cs +++ /dev/null @@ -1,129 +0,0 @@ -//----------------------------------------------------------------------------- -// -// Copyright (C) Microsoft Corporation. All Rights Reserved. -// -//----------------------------------------------------------------------------- -using System; -using System.IO; - -namespace Microsoft.Cci.Pdb { - internal class PdbWriter { - internal PdbWriter(Stream writer, int pageSize) { - this.pageSize = pageSize; - this.usedBytes = pageSize * 3; - this.writer = writer; - - writer.SetLength(usedBytes); - } - - internal void WriteMeta(DataStream[] streams, BitAccess bits) { - PdbFileHeader head = new PdbFileHeader(pageSize); - - WriteDirectory(streams, - out head.directoryRoot, - out head.directorySize, - bits); - WriteFreeMap(); - - head.freePageMap = 2; - head.pagesUsed = usedBytes / pageSize; - - writer.Seek(0, SeekOrigin.Begin); - head.Write(writer, bits); - } - - private void WriteDirectory(DataStream[] streams, - out int directoryRoot, - out int directorySize, - BitAccess bits) { - DataStream directory = new DataStream(); - - int pages = 0; - for (int s = 0; s < streams.Length; s++) { - if (streams[s].Length > 0) { - pages += streams[s].Pages; - } - } - - int use = 4 * (1 + streams.Length + pages); - bits.MinCapacity(use); - bits.WriteInt32(streams.Length); - for (int s = 0; s < streams.Length; s++) { - bits.WriteInt32(streams[s].Length); - } - for (int s = 0; s < streams.Length; s++) { - if (streams[s].Length > 0) { - bits.WriteInt32(streams[s].pages); - } - } - directory.Write(this, bits.Buffer, use); - directorySize = directory.Length; - - use = 4 * directory.Pages; - bits.MinCapacity(use); - bits.WriteInt32(directory.pages); - - DataStream ddir = new DataStream(); - ddir.Write(this, bits.Buffer, use); - - directoryRoot = ddir.pages[0]; - } - - private void WriteFreeMap() { - byte[] buffer = new byte[pageSize]; - - // We configure the old free map with only the first 3 pages allocated. - buffer[0] = 0xf8; - for (int i = 1; i < pageSize; i++) { - buffer[i] = 0xff; - } - Seek(1, 0); - Write(buffer, 0, pageSize); - - // We configure the new free map with all of the used pages gone. - int count = usedBytes / pageSize; - int full = count / 8; - for (int i = 0; i < full; i++) { - buffer[i] = 0; - } - int rema = count % 8; - buffer[full] = (byte)(0xff << rema); - - Seek(2, 0); - Write(buffer, 0, pageSize); - } - - internal int AllocatePages(int count) { - int begin = usedBytes; - - usedBytes += count * pageSize; - writer.SetLength(usedBytes); - - if (usedBytes > pageSize * pageSize * 8) { - throw new Exception("PdbWriter does not support multiple free maps."); - } - return begin / pageSize; - } - - internal void Seek(int page, int offset) { - writer.Seek(page * pageSize + offset, SeekOrigin.Begin); - } - - internal void Write(byte[] bytes, int offset, int count) { - writer.Write(bytes, offset, count); - } - - ////////////////////////////////////////////////////////////////////// - // - internal int PageSize { - get { return pageSize; } - } - - ////////////////////////////////////////////////////////////////////// - // - internal readonly int pageSize; - private Stream writer; - private int usedBytes; - } - -} diff --git a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/SourceLocationProvider.cs b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/SourceLocationProvider.cs index 2b28971cb..db3f291b4 100644 --- a/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/SourceLocationProvider.cs +++ b/Mono.Cecil/symbols/pdb/Microsoft.Cci.Pdb/SourceLocationProvider.cs @@ -13,38 +13,6 @@ using System.Diagnostics.SymbolStore; namespace Microsoft.Cci { - internal sealed class UsedNamespace : IUsedNamespace { - - internal UsedNamespace(IName alias, IName namespaceName) { - this.alias = alias; - this.namespaceName = namespaceName; - } - - public IName Alias { - get { return this.alias; } - } - readonly IName alias; - - public IName NamespaceName { - get { return this.namespaceName; } - } - readonly IName namespaceName; - - } - - internal class NamespaceScope : INamespaceScope { - - internal NamespaceScope(IEnumerable usedNamespaces) { - this.usedNamespaces = usedNamespaces; - } - - public IEnumerable UsedNamespaces { - get { return this.usedNamespaces; } - } - readonly IEnumerable usedNamespaces; - - } - internal sealed class PdbIteratorScope : ILocalScope { internal PdbIteratorScope(uint offset, uint length) { @@ -61,6 +29,5 @@ namespace Microsoft.Cci { get { return this.length; } } uint length; - } } \ No newline at end of file diff --git a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb.csproj b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb.csproj index 2d2dfd273..5248bad35 100644 --- a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb.csproj +++ b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb.csproj @@ -1,4 +1,4 @@ - + net_4_0_Debug @@ -108,7 +108,6 @@ - diff --git a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/AssemblyInfo.cs b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/AssemblyInfo.cs index ea63ba2ac..14d267990 100644 --- a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/AssemblyInfo.cs +++ b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/AssemblyInfo.cs @@ -32,7 +32,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle ("Mono.Cecil.Pdb")] [assembly: AssemblyProduct ("Mono.Cecil")] -[assembly: AssemblyCopyright ("Copyright © 2008 - 2010 Jb Evain")] +[assembly: AssemblyCopyright ("Copyright © 2008 - 2011 Jb Evain")] [assembly: CLSCompliant (false)] [assembly: ComVisible (false)] diff --git a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/ModuleMetadata.cs b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/ModuleMetadata.cs index 6509f0773..e06f12aef 100644 --- a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/ModuleMetadata.cs +++ b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/ModuleMetadata.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; @@ -75,7 +76,7 @@ namespace Mono.Cecil.Pdb { uint FindTypeDefByName (string szTypeDef, uint tkEnclosingClass); Guid GetScopeProps (StringBuilder szName, uint cchName, out uint pchName); uint GetModuleFromScope (); - uint GetTypeDefProps (uint td, IntPtr szTypeDef, uint cchTypeDef, out uint pchTypeDef, ref uint pdwTypeDefFlags); + uint GetTypeDefProps (uint td, IntPtr szTypeDef, uint cchTypeDef, out uint pchTypeDef, IntPtr pdwTypeDefFlags); uint GetInterfaceImplProps (uint iiImpl, out uint pClass); uint GetTypeRefProps (uint tr, out uint ptkResolutionScope, StringBuilder szName, uint cchName); uint ResolveTypeRef (uint tr, [In] ref Guid riid, [MarshalAs (UnmanagedType.Interface)] out object ppIScope); @@ -95,8 +96,7 @@ namespace Mono.Cecil.Pdb { uint FindMethod (uint td, string szName, [MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 3)] byte [] pvSigBlob, uint cbSigBlob); uint FindField (uint td, string szName, [MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 3)] byte [] pvSigBlob, uint cbSigBlob); uint FindMemberRef (uint td, string szName, [MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 3)] byte [] pvSigBlob, uint cbSigBlob); - uint GetMethodProps (uint mb, out uint pClass, IntPtr szMethod, uint cchMethod, out uint pchMethod, IntPtr pdwAttr, - IntPtr ppvSigBlob, IntPtr pcbSigBlob, IntPtr pulCodeRVA); + uint GetMethodProps (uint mb, out uint pClass, IntPtr szMethod, uint cchMethod, out uint pchMethod, IntPtr pdwAttr, IntPtr ppvSigBlob, IntPtr pcbSigBlob, IntPtr pulCodeRVA); uint GetMemberRefProps (uint mr, ref uint ptk, StringBuilder szMember, uint cchMember, out uint pchMember, out IntPtr /* byte* */ ppvSigBlob); uint EnumProperties (ref uint phEnum, uint td, IntPtr /* uint* */ rProperties, uint cMax); uint EnumEvents (ref uint phEnum, uint td, IntPtr /* uint* */ rEvents, uint cMax); @@ -147,11 +147,47 @@ namespace Mono.Cecil.Pdb { readonly ModuleDefinition module; + Dictionary types; + Dictionary methods; + public ModuleMetadata (ModuleDefinition module) { this.module = module; } + bool TryGetType (uint token, out TypeDefinition type) + { + if (types == null) + InitializeMetadata (module); + + return types.TryGetValue (token, out type); + } + + bool TryGetMethod (uint token, out MethodDefinition method) + { + if (methods == null) + InitializeMetadata (module); + + return methods.TryGetValue (token, out method); + } + + void InitializeMetadata (ModuleDefinition module) + { + types = new Dictionary (); + methods = new Dictionary (); + + foreach (var type in module.GetTypes ()) { + types.Add (type.MetadataToken.ToUInt32 (), type); + InitializeMethods (type); + } + } + + void InitializeMethods (TypeDefinition type) + { + foreach (var method in type.Methods) + methods.Add (method.MetadataToken.ToUInt32 (), method); + } + public void SetModuleProps (string szName) { throw new NotImplementedException (); @@ -442,10 +478,40 @@ namespace Mono.Cecil.Pdb { throw new NotImplementedException (); } - public uint GetTypeDefProps (uint td, IntPtr szTypeDef, uint cchTypeDef, out uint pchTypeDef, ref uint pdwTypeDefFlags) + public uint GetTypeDefProps (uint td, IntPtr szTypeDef, uint cchTypeDef, out uint pchTypeDef, IntPtr pdwTypeDefFlags) + { + TypeDefinition type; + if (!TryGetType (td, out type)) { + Marshal.WriteInt16 (szTypeDef, 0); + pchTypeDef = 1; + return 0; + } + + WriteString (type.Name, szTypeDef, cchTypeDef, out pchTypeDef); + WriteIntPtr (pdwTypeDefFlags, (uint) type.Attributes); + return type.BaseType != null ? type.BaseType.MetadataToken.ToUInt32 () : 0; + } + + static void WriteIntPtr (IntPtr ptr, uint value) { - pchTypeDef = 0; - return td; + if (ptr == IntPtr.Zero) + return; + + Marshal.WriteInt32 (ptr, (int) value); + } + + static void WriteString (string str, IntPtr buffer, uint bufferSize, out uint chars) + { + var length = str.Length + 1 >= bufferSize ? bufferSize - 1 : (uint) str.Length; + chars = length + 1; + var offset = 0; + + for (int i = 0; i < length; i++) { + Marshal.WriteInt16 (buffer, offset, str [i]); + offset += 2; + } + + Marshal.WriteInt16 (buffer, offset, 0); } public uint GetInterfaceImplProps (uint iiImpl, out uint pClass) @@ -535,9 +601,20 @@ namespace Mono.Cecil.Pdb { public uint GetMethodProps (uint mb, out uint pClass, IntPtr szMethod, uint cchMethod, out uint pchMethod, IntPtr pdwAttr, IntPtr ppvSigBlob, IntPtr pcbSigBlob, IntPtr pulCodeRVA) { - pClass = 0; - pchMethod = 0; - return mb; + MethodDefinition method; + if (!TryGetMethod (mb, out method)) { + Marshal.WriteInt16 (szMethod, 0); + pchMethod = 1; + pClass = 0; + return 0; + } + + pClass = method.DeclaringType.MetadataToken.ToUInt32 (); + WriteString (method.Name, szMethod, cchMethod, out pchMethod); + WriteIntPtr (pdwAttr, (uint) method.Attributes); + WriteIntPtr (pulCodeRVA, (uint) method.RVA); + + return (uint) method.ImplAttributes; } public uint GetMemberRefProps (uint mr, ref uint ptk, StringBuilder szMember, uint cchMember, out uint pchMember, out IntPtr ppvSigBlob) @@ -697,7 +774,11 @@ namespace Mono.Cecil.Pdb { public uint GetNestedClassProps (uint tdNestedClass) { - throw new NotImplementedException (); + TypeDefinition type; + if (!TryGetType (tdNestedClass, out type)) + return 0; + + return type.IsNested ? type.DeclaringType.MetadataToken.ToUInt32 () : 0; } public uint GetNativeCallConvFromSig (IntPtr pvSig, uint cbSig) diff --git a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs index 79e82dbd5..aa6398253 100644 --- a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs +++ b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs @@ -104,6 +104,7 @@ namespace Mono.Cecil.Pdb { AddMapping (DocumentLanguage.JScript, new Guid (0x3a12d0b6, 0xc26c, 0x11d0, 0xb4, 0x42, 0x0, 0xa0, 0x24, 0x4a, 0x1d, 0xd2)); AddMapping (DocumentLanguage.Smc, new Guid (0xd9b9f7b, 0x6611, 0x11d3, 0xbd, 0x2a, 0x0, 0x0, 0xf8, 0x8, 0x49, 0xbd)); AddMapping (DocumentLanguage.MCpp, new Guid (0x4b35fde8, 0x07c6, 0x11d3, 0x90, 0x53, 0x0, 0xc0, 0x4f, 0xa3, 0x02, 0xa1)); + AddMapping (DocumentLanguage.FSharp, new Guid (0xab4f38c9, 0xb6e6, 0x43ba, 0xbe, 0x3b, 0x58, 0x08, 0x0b, 0x2c, 0xcc, 0xe3)); } static void AddMapping (DocumentLanguage language, Guid guid) diff --git a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbReader.cs b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbReader.cs index cd3c7f46e..9d7166638 100644 --- a/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbReader.cs +++ b/Mono.Cecil/symbols/pdb/Mono.Cecil.Pdb/PdbReader.cs @@ -90,12 +90,8 @@ namespace Mono.Cecil.Pdb { Guid guid; var funcs = PdbFile.LoadFunctions (pdb_file, true, out age, out guid); - if (this.age != 0) { - if (this.age != age) - return false; - if (this.guid != guid) - return false; - } + if (this.age != 0 && this.guid != guid) + return false; foreach (PdbFunction function in funcs) functions.Add (function.token, function); diff --git a/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj b/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj index 68fef1924..7bf3ed40b 100644 --- a/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj +++ b/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj @@ -92,21 +92,25 @@ + + + + - + False - ..\..\..\Test\libs\nunit-2.4.8\nunit.core.dll + ..\..\..\Test\libs\nunit-2.5.10\nunit.core.dll - + False - ..\..\..\Test\libs\nunit-2.4.8\nunit.core.interfaces.dll + ..\..\..\Test\libs\nunit-2.5.10\nunit.core.interfaces.dll - + False - ..\..\..\Test\libs\nunit-2.4.8\nunit.framework.dll + ..\..\..\Test\libs\nunit-2.5.10\nunit.framework.dll diff --git a/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Tests/PdbTests.cs b/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Tests/PdbTests.cs index 60d93a40c..d084dcd44 100644 --- a/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Tests/PdbTests.cs +++ b/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Tests/PdbTests.cs @@ -76,6 +76,42 @@ namespace Mono.Cecil.Tests { Assert.AreEqual (DocumentLanguageVendor.Microsoft, document.LanguageVendor); } + [TestModule ("VBConsApp.exe", SymbolReaderProvider = typeof (PdbReaderProvider), SymbolWriterProvider = typeof (PdbWriterProvider))] + public void BasicDocument (ModuleDefinition module) + { + var type = module.GetType ("VBConsApp.Program"); + var method = type.GetMethod ("Main"); + + var sequence_point = method.Body.Instructions.Where (i => i.SequencePoint != null).First ().SequencePoint; + var document = sequence_point.Document; + + Assert.IsNotNull (document); + + Assert.AreEqual (@"c:\tmp\VBConsApp\Program.vb", document.Url); + Assert.AreEqual (DocumentType.Text, document.Type); + Assert.AreEqual (DocumentHashAlgorithm.None, document.HashAlgorithm); + Assert.AreEqual (DocumentLanguage.Basic, document.Language); + Assert.AreEqual (DocumentLanguageVendor.Microsoft, document.LanguageVendor); + } + + [TestModule ("fsapp.exe", SymbolReaderProvider = typeof (PdbReaderProvider), SymbolWriterProvider = typeof (PdbWriterProvider))] + public void FSharpDocument (ModuleDefinition module) + { + var type = module.GetType ("Program"); + var method = type.GetMethod ("fact"); + + var sequence_point = method.Body.Instructions.Where (i => i.SequencePoint != null).First ().SequencePoint; + var document = sequence_point.Document; + + Assert.IsNotNull (document); + + Assert.AreEqual (@"c:\tmp\fsapp\Program.fs", document.Url); + Assert.AreEqual (DocumentType.Text, document.Type); + Assert.AreEqual (DocumentHashAlgorithm.None, document.HashAlgorithm); + Assert.AreEqual (DocumentLanguage.FSharp, document.Language); + Assert.AreEqual (DocumentLanguageVendor.Microsoft, document.LanguageVendor); + } + [Test] public void CreateMethodFromScratch () { diff --git a/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/VBConsApp.exe b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/VBConsApp.exe new file mode 100755 index 000000000..c86b64b15 Binary files /dev/null and b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/VBConsApp.exe differ diff --git a/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/VBConsApp.pdb b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/VBConsApp.pdb new file mode 100755 index 000000000..2625666b4 Binary files /dev/null and b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/VBConsApp.pdb differ diff --git a/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/fsapp.exe b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/fsapp.exe new file mode 100755 index 000000000..7cdd2368e Binary files /dev/null and b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/fsapp.exe differ diff --git a/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/fsapp.pdb b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/fsapp.pdb new file mode 100755 index 000000000..3743d24be Binary files /dev/null and b/Mono.Cecil/symbols/pdb/Test/Resources/assemblies/fsapp.pdb differ