Browse Source

Make Cecil property getters thread-safe.

pull/37/head
Daniel Grunwald 15 years ago
parent
commit
32bd96922e
  1. 6
      Mono.Cecil/Mono.Cecil/AssemblyDefinition.cs
  2. 7
      Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs
  3. 3
      Mono.Cecil/Mono.Cecil/AssemblyReader.cs
  4. 33
      Mono.Cecil/Mono.Cecil/CustomAttribute.cs
  5. 20
      Mono.Cecil/Mono.Cecil/EventDefinition.cs
  6. 4
      Mono.Cecil/Mono.Cecil/FieldDefinition.cs
  7. 4
      Mono.Cecil/Mono.Cecil/GenericParameter.cs
  8. 7
      Mono.Cecil/Mono.Cecil/IConstantProvider.cs
  9. 6
      Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs
  10. 5
      Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs
  11. 3
      Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs
  12. 26
      Mono.Cecil/Mono.Cecil/MethodDefinition.cs
  13. 50
      Mono.Cecil/Mono.Cecil/ModuleDefinition.cs
  14. 4
      Mono.Cecil/Mono.Cecil/ParameterDefinition.cs
  15. 16
      Mono.Cecil/Mono.Cecil/PropertyDefinition.cs
  16. 7
      Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs
  17. 18
      Mono.Cecil/Mono.Cecil/TypeDefinition.cs
  18. 61
      Mono.Cecil/Mono.Cecil/TypeSystem.cs

6
Mono.Cecil/Mono.Cecil/AssemblyDefinition.cs

@ -62,7 +62,7 @@ namespace Mono.Cecil {
return modules; return modules;
if (main_module.HasImage) if (main_module.HasImage)
return modules = main_module.Read (this, (_, reader) => reader.ReadModules ()); return main_module.Read (ref modules, this, (_, reader) => reader.ReadModules ());
return modules = new Collection<ModuleDefinition> { main_module }; return modules = new Collection<ModuleDefinition> { main_module };
} }
@ -87,7 +87,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (main_module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, main_module)); }
} }
public bool HasSecurityDeclarations { public bool HasSecurityDeclarations {
@ -100,7 +100,7 @@ namespace Mono.Cecil {
} }
public Collection<SecurityDeclaration> SecurityDeclarations { public Collection<SecurityDeclaration> SecurityDeclarations {
get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (main_module)); } get { return security_declarations ?? (this.GetSecurityDeclarations (ref security_declarations, main_module)); }
} }
internal AssemblyDefinition () internal AssemblyDefinition ()

7
Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs

@ -107,9 +107,10 @@ namespace Mono.Cecil {
if (public_key_token.IsNullOrEmpty () && !public_key.IsNullOrEmpty ()) { if (public_key_token.IsNullOrEmpty () && !public_key.IsNullOrEmpty ()) {
var hash = HashPublicKey (); var hash = HashPublicKey ();
// we need the last 8 bytes in reverse order // we need the last 8 bytes in reverse order
public_key_token = new byte [8]; byte[] local_public_key_token = new byte [8];
Array.Copy (hash, (hash.Length - 8), public_key_token, 0, 8); Array.Copy (hash, (hash.Length - 8), local_public_key_token, 0, 8);
Array.Reverse (public_key_token, 0, 8); Array.Reverse (local_public_key_token, 0, 8);
public_key_token = local_public_key_token; // publish only once finished (required for thread-safety)
} }
return public_key_token; return public_key_token;
} }

3
Mono.Cecil/Mono.Cecil/AssemblyReader.cs

@ -1558,10 +1558,11 @@ namespace Mono.Cecil {
var methods = type.Methods; var methods = type.Methods;
for (int i = 0; i < methods.Count; i++) { for (int i = 0; i < methods.Count; i++) {
var method = methods [i]; var method = methods [i];
if (method.sem_attrs.HasValue) if (method.sem_attrs_ready)
continue; continue;
method.sem_attrs = ReadMethodSemantics (method); method.sem_attrs = ReadMethodSemantics (method);
method.sem_attrs_ready = true;
} }
} }

33
Mono.Cecil/Mono.Cecil/CustomAttribute.cs

@ -191,7 +191,7 @@ namespace Mono.Cecil {
if (!HasImage || signature == 0) if (!HasImage || signature == 0)
throw new NotSupportedException (); throw new NotSupportedException ();
return blob = Module.Read (this, (attribute, reader) => reader.ReadCustomAttributeBlob (attribute.signature)); return Module.Read (ref blob, this, (attribute, reader) => reader.ReadCustomAttributeBlob (attribute.signature));
} }
void Resolve () void Resolve ()
@ -199,23 +199,22 @@ namespace Mono.Cecil {
if (resolved || !HasImage) if (resolved || !HasImage)
return; return;
try { Module.Read (this, (attribute, reader) => {
Module.Read (this, (attribute, reader) => { try {
reader.ReadCustomAttributeSignature (attribute); reader.ReadCustomAttributeSignature (attribute);
return this; resolved = true;
}); } catch (ResolutionException) {
if (arguments != null)
resolved = true; arguments.Clear ();
} catch (ResolutionException) { if (fields != null)
if (arguments != null) fields.Clear ();
arguments.Clear (); if (properties != null)
if (fields != null) properties.Clear ();
fields.Clear ();
if (properties != null) resolved = false;
properties.Clear (); }
return this;
resolved = false; });
}
} }
} }

20
Mono.Cecil/Mono.Cecil/EventDefinition.cs

@ -113,7 +113,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
} }
#region EventAttributes #region EventAttributes
@ -148,16 +148,18 @@ namespace Mono.Cecil {
void InitializeMethods () void InitializeMethods ()
{ {
if (add_method != null
|| invoke_method != null
|| remove_method != null)
return;
var module = this.Module; var module = this.Module;
if (!module.HasImage ()) lock (module.SyncRoot) {
return; if (add_method != null
|| invoke_method != null
|| remove_method != null)
return;
if (!module.HasImage ())
return;
module.Read (this, (@event, reader) => reader.ReadMethods (@event)); module.Read (this, (@event, reader) => reader.ReadMethods (@event));
}
} }
public override EventDefinition Resolve () public override EventDefinition Resolve ()

4
Mono.Cecil/Mono.Cecil/FieldDefinition.cs

@ -154,7 +154,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
} }
public bool HasMarshalInfo { public bool HasMarshalInfo {
@ -167,7 +167,7 @@ namespace Mono.Cecil {
} }
public MarshalInfo MarshalInfo { public MarshalInfo MarshalInfo {
get { return marshal_info ?? (marshal_info = this.GetMarshalInfo (Module)); } get { return marshal_info ?? (this.GetMarshalInfo (ref marshal_info, Module)); }
set { marshal_info = value; } set { marshal_info = value; }
} }

4
Mono.Cecil/Mono.Cecil/GenericParameter.cs

@ -78,7 +78,7 @@ namespace Mono.Cecil {
return constraints; return constraints;
if (HasImage) if (HasImage)
return constraints = Module.Read (this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter)); return Module.Read (ref constraints, this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter));
return constraints = new Collection<TypeReference> (); return constraints = new Collection<TypeReference> ();
} }
@ -94,7 +94,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
} }
internal new bool HasImage { internal new bool HasImage {

7
Mono.Cecil/Mono.Cecil/IConstantProvider.cs

@ -44,9 +44,10 @@ namespace Mono.Cecil {
ref object constant, ref object constant,
ModuleDefinition module) ModuleDefinition module)
{ {
constant = module.HasImage () if (module.HasImage ())
? module.Read (self, (provider, reader) => reader.ReadConstant (provider)) module.Read (ref constant, self, (provider, reader) => reader.ReadConstant (provider));
: Mixin.NoValue; else
constant = Mixin.NoValue;
} }
} }
} }

6
Mono.Cecil/Mono.Cecil/ICustomAttributeProvider.cs

@ -27,7 +27,6 @@
// //
using System; using System;
using Mono.Collections.Generic; using Mono.Collections.Generic;
namespace Mono.Cecil { namespace Mono.Cecil {
@ -52,11 +51,12 @@ namespace Mono.Cecil {
public static Collection<CustomAttribute> GetCustomAttributes ( public static Collection<CustomAttribute> GetCustomAttributes (
this ICustomAttributeProvider self, this ICustomAttributeProvider self,
ref Collection<CustomAttribute> variable,
ModuleDefinition module) ModuleDefinition module)
{ {
return module.HasImage () return module.HasImage ()
? module.Read (self, (provider, reader) => reader.ReadCustomAttributes (provider)) ? module.Read (ref variable, self, (provider, reader) => reader.ReadCustomAttributes (provider))
: new Collection<CustomAttribute> (); : variable = new Collection<CustomAttribute>();
} }
} }
} }

5
Mono.Cecil/Mono.Cecil/IGenericParameterProvider.cs

@ -65,11 +65,12 @@ namespace Mono.Cecil {
public static Collection<GenericParameter> GetGenericParameters ( public static Collection<GenericParameter> GetGenericParameters (
this IGenericParameterProvider self, this IGenericParameterProvider self,
ref Collection<GenericParameter> collection,
ModuleDefinition module) ModuleDefinition module)
{ {
return module.HasImage () return module.HasImage ()
? module.Read (self, (provider, reader) => reader.ReadGenericParameters (provider)) ? module.Read (ref collection, self, (provider, reader) => reader.ReadGenericParameters (provider))
: new Collection<GenericParameter> (); : collection = new Collection<GenericParameter> ();
} }
} }
} }

3
Mono.Cecil/Mono.Cecil/IMarshalInfoProvider.cs

@ -47,10 +47,11 @@ namespace Mono.Cecil {
public static MarshalInfo GetMarshalInfo ( public static MarshalInfo GetMarshalInfo (
this IMarshalInfoProvider self, this IMarshalInfoProvider self,
ref MarshalInfo variable,
ModuleDefinition module) ModuleDefinition module)
{ {
return module.HasImage () return module.HasImage ()
? module.Read (self, (provider, reader) => reader.ReadMarshalInfo (provider)) ? module.Read (ref variable, self, (provider, reader) => reader.ReadMarshalInfo (provider))
: null; : null;
} }
} }

26
Mono.Cecil/Mono.Cecil/MethodDefinition.cs

@ -37,7 +37,8 @@ namespace Mono.Cecil {
ushort attributes; ushort attributes;
ushort impl_attributes; ushort impl_attributes;
internal MethodSemanticsAttributes? sem_attrs; internal volatile bool sem_attrs_ready;
internal MethodSemanticsAttributes sem_attrs;
Collection<CustomAttribute> custom_attributes; Collection<CustomAttribute> custom_attributes;
Collection<SecurityDeclaration> security_declarations; Collection<SecurityDeclaration> security_declarations;
@ -59,23 +60,24 @@ namespace Mono.Cecil {
public MethodSemanticsAttributes SemanticsAttributes { public MethodSemanticsAttributes SemanticsAttributes {
get { get {
if (sem_attrs.HasValue) if (sem_attrs_ready)
return sem_attrs.Value; return sem_attrs;
if (HasImage) { if (HasImage) {
ReadSemantics (); ReadSemantics ();
return sem_attrs.Value; return sem_attrs;
} }
sem_attrs = MethodSemanticsAttributes.None; sem_attrs = MethodSemanticsAttributes.None;
return sem_attrs.Value; sem_attrs_ready = true;
return sem_attrs;
} }
set { sem_attrs = value; } set { sem_attrs = value; }
} }
internal void ReadSemantics () internal void ReadSemantics ()
{ {
if (sem_attrs.HasValue) if (sem_attrs_ready)
return; return;
var module = this.Module; var module = this.Module;
@ -98,7 +100,7 @@ namespace Mono.Cecil {
} }
public Collection<SecurityDeclaration> SecurityDeclarations { public Collection<SecurityDeclaration> SecurityDeclarations {
get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (Module)); } get { return security_declarations ?? (this.GetSecurityDeclarations (ref security_declarations, Module)); }
} }
public bool HasCustomAttributes { public bool HasCustomAttributes {
@ -111,7 +113,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
} }
public int RVA { public int RVA {
@ -138,7 +140,7 @@ namespace Mono.Cecil {
return null; return null;
if (HasImage && rva != 0) if (HasImage && rva != 0)
return body = Module.Read (this, (method, reader) => reader.ReadMethodBody (method)); return Module.Read (ref body, this, (method, reader) => reader.ReadMethodBody (method));
return body = new MethodBody (this); return body = new MethodBody (this);
} }
@ -160,7 +162,7 @@ namespace Mono.Cecil {
return pinvoke; return pinvoke;
if (HasImage && IsPInvokeImpl) if (HasImage && IsPInvokeImpl)
return pinvoke = Module.Read (this, (method, reader) => reader.ReadPInvokeInfo (method)); return Module.Read (ref pinvoke, this, (method, reader) => reader.ReadPInvokeInfo (method));
return null; return null;
} }
@ -188,7 +190,7 @@ namespace Mono.Cecil {
return overrides; return overrides;
if (HasImage) if (HasImage)
return overrides = Module.Read (this, (method, reader) => reader.ReadOverrides (method)); return Module.Read (ref overrides, this, (method, reader) => reader.ReadOverrides (method));
return overrides = new Collection<MethodReference> (); return overrides = new Collection<MethodReference> ();
} }
@ -204,7 +206,7 @@ namespace Mono.Cecil {
} }
public override Collection<GenericParameter> GenericParameters { public override Collection<GenericParameter> GenericParameters {
get { return generic_parameters ?? (generic_parameters = this.GetGenericParameters (Module)); } get { return generic_parameters ?? (this.GetGenericParameters (ref generic_parameters, Module)); }
} }
#region MethodAttributes #region MethodAttributes

50
Mono.Cecil/Mono.Cecil/ModuleDefinition.cs

@ -29,8 +29,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading;
using SR = System.Reflection; using SR = System.Reflection;
using Mono.Cecil.Cil; using Mono.Cecil.Cil;
using Mono.Cecil.Metadata; using Mono.Cecil.Metadata;
using Mono.Cecil.PE; using Mono.Cecil.PE;
@ -261,7 +261,11 @@ namespace Mono.Cecil {
#if !READ_ONLY #if !READ_ONLY
internal MetadataImporter MetadataImporter { internal MetadataImporter MetadataImporter {
get { return importer ?? (importer = new MetadataImporter (this)); } get {
if (importer == null)
Interlocked.CompareExchange(ref importer, new MetadataImporter(this), null);
return importer;
}
} }
#endif #endif
@ -270,7 +274,11 @@ namespace Mono.Cecil {
} }
public TypeSystem TypeSystem { public TypeSystem TypeSystem {
get { return type_system ?? (type_system = TypeSystem.CreateTypeSystem (this)); } get {
if (type_system == null)
Interlocked.CompareExchange(ref type_system, TypeSystem.CreateTypeSystem (this), null);
return type_system;
}
} }
public bool HasAssemblyReferences { public bool HasAssemblyReferences {
@ -288,7 +296,7 @@ namespace Mono.Cecil {
return references; return references;
if (HasImage) if (HasImage)
return references = Read (this, (_, reader) => reader.ReadAssemblyReferences ()); return Read (ref references, this, (_, reader) => reader.ReadAssemblyReferences ());
return references = new Collection<AssemblyNameReference> (); return references = new Collection<AssemblyNameReference> ();
} }
@ -309,7 +317,7 @@ namespace Mono.Cecil {
return modules; return modules;
if (HasImage) if (HasImage)
return modules = Read (this, (_, reader) => reader.ReadModuleReferences ()); return Read (ref modules, this, (_, reader) => reader.ReadModuleReferences ());
return modules = new Collection<ModuleReference> (); return modules = new Collection<ModuleReference> ();
} }
@ -333,7 +341,7 @@ namespace Mono.Cecil {
return resources; return resources;
if (HasImage) if (HasImage)
return resources = Read (this, (_, reader) => reader.ReadResources ()); return Read (ref resources, this, (_, reader) => reader.ReadResources ());
return resources = new Collection<Resource> (); return resources = new Collection<Resource> ();
} }
@ -349,7 +357,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (this)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, this)); }
} }
public bool HasTypes { public bool HasTypes {
@ -367,7 +375,7 @@ namespace Mono.Cecil {
return types; return types;
if (HasImage) if (HasImage)
return types = Read (this, (_, reader) => reader.ReadTypes ()); return Read (ref types, this, (_, reader) => reader.ReadTypes ());
return types = new TypeDefinitionCollection (this); return types = new TypeDefinitionCollection (this);
} }
@ -388,7 +396,7 @@ namespace Mono.Cecil {
return exported_types; return exported_types;
if (HasImage) if (HasImage)
return exported_types = Read (this, (_, reader) => reader.ReadExportedTypes ()); return Read (ref exported_types, this, (_, reader) => reader.ReadExportedTypes ());
return exported_types = new Collection<ExportedType> (); return exported_types = new Collection<ExportedType> ();
} }
@ -400,7 +408,7 @@ namespace Mono.Cecil {
return entry_point; return entry_point;
if (HasImage) if (HasImage)
return entry_point = Read (this, (_, reader) => reader.ReadEntryPoint ()); return Read (ref entry_point, this, (_, reader) => reader.ReadEntryPoint ());
return entry_point = null; return entry_point = null;
} }
@ -760,6 +768,10 @@ namespace Mono.Cecil {
readonly object module_lock = new object(); readonly object module_lock = new object();
internal object SyncRoot {
get { return module_lock; }
}
internal TRet Read<TItem, TRet> (TItem item, Func<TItem, MetadataReader, TRet> read) internal TRet Read<TItem, TRet> (TItem item, Func<TItem, MetadataReader, TRet> read)
{ {
lock (module_lock) { lock (module_lock) {
@ -774,6 +786,24 @@ namespace Mono.Cecil {
return ret; return ret;
} }
} }
internal TRet Read<TItem, TRet> (ref TRet variable, TItem item, Func<TItem, MetadataReader, TRet> read) where TRet : class
{
lock (module_lock) {
if (variable != null)
return variable;
var position = reader.position;
var context = reader.context;
var ret = read (item, reader);
reader.position = position;
reader.context = context;
return variable = ret;
}
}
void ProcessDebugHeader () void ProcessDebugHeader ()
{ {

4
Mono.Cecil/Mono.Cecil/ParameterDefinition.cs

@ -81,7 +81,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (parameter_type.Module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, parameter_type.Module)); }
} }
public bool HasMarshalInfo { public bool HasMarshalInfo {
@ -94,7 +94,7 @@ namespace Mono.Cecil {
} }
public MarshalInfo MarshalInfo { public MarshalInfo MarshalInfo {
get { return marshal_info ?? (marshal_info = this.GetMarshalInfo (parameter_type.Module)); } get { return marshal_info ?? (this.GetMarshalInfo (ref marshal_info, parameter_type.Module)); }
set { marshal_info = value; } set { marshal_info = value; }
} }

16
Mono.Cecil/Mono.Cecil/PropertyDefinition.cs

@ -76,7 +76,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
} }
public MethodDefinition GetMethod { public MethodDefinition GetMethod {
@ -245,14 +245,16 @@ namespace Mono.Cecil {
void InitializeMethods () void InitializeMethods ()
{ {
if (get_method != null || set_method != null)
return;
var module = this.Module; var module = this.Module;
if (!module.HasImage ()) lock (module.SyncRoot) {
return; if (get_method != null || set_method != null)
return;
module.Read (this, (property, reader) => reader.ReadMethods (property)); if (!module.HasImage ())
return;
module.Read (this, (property, reader) => reader.ReadMethods (property));
}
} }
public override PropertyDefinition Resolve () public override PropertyDefinition Resolve ()

7
Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs

@ -27,7 +27,7 @@
// //
using System; using System;
using System.Threading;
using Mono.Collections.Generic; using Mono.Collections.Generic;
namespace Mono.Cecil { namespace Mono.Cecil {
@ -172,11 +172,12 @@ namespace Mono.Cecil {
public static Collection<SecurityDeclaration> GetSecurityDeclarations ( public static Collection<SecurityDeclaration> GetSecurityDeclarations (
this ISecurityDeclarationProvider self, this ISecurityDeclarationProvider self,
ref Collection<SecurityDeclaration> variable,
ModuleDefinition module) ModuleDefinition module)
{ {
return module.HasImage () return module.HasImage ()
? module.Read (self, (provider, reader) => reader.ReadSecurityDeclarations (provider)) ? module.Read (ref variable, self, (provider, reader) => reader.ReadSecurityDeclarations (provider))
: new Collection<SecurityDeclaration> (); : LazyInitializer.EnsureInitialized(ref variable);
} }
} }
} }

18
Mono.Cecil/Mono.Cecil/TypeDefinition.cs

@ -131,7 +131,7 @@ namespace Mono.Cecil {
return interfaces; return interfaces;
if (HasImage) if (HasImage)
return interfaces = Module.Read (this, (type, reader) => reader.ReadInterfaces (type)); return Module.Read (ref interfaces, this, (type, reader) => reader.ReadInterfaces (type));
return interfaces = new Collection<TypeReference> (); return interfaces = new Collection<TypeReference> ();
} }
@ -155,7 +155,7 @@ namespace Mono.Cecil {
return nested_types; return nested_types;
if (HasImage) if (HasImage)
return nested_types = Module.Read (this, (type, reader) => reader.ReadNestedTypes (type)); return Module.Read (ref nested_types, this, (type, reader) => reader.ReadNestedTypes (type));
return nested_types = new MemberDefinitionCollection<TypeDefinition> (this); return nested_types = new MemberDefinitionCollection<TypeDefinition> (this);
} }
@ -183,7 +183,7 @@ namespace Mono.Cecil {
return methods; return methods;
if (HasImage) if (HasImage)
return methods = Module.Read (this, (type, reader) => reader.ReadMethods (type)); return Module.Read (ref methods, this, (type, reader) => reader.ReadMethods (type));
return methods = new MemberDefinitionCollection<MethodDefinition> (this); return methods = new MemberDefinitionCollection<MethodDefinition> (this);
} }
@ -207,7 +207,7 @@ namespace Mono.Cecil {
return fields; return fields;
if (HasImage) if (HasImage)
return fields = Module.Read (this, (type, reader) => reader.ReadFields (type)); return Module.Read (ref fields, this, (type, reader) => reader.ReadFields (type));
return fields = new MemberDefinitionCollection<FieldDefinition> (this); return fields = new MemberDefinitionCollection<FieldDefinition> (this);
} }
@ -231,7 +231,7 @@ namespace Mono.Cecil {
return events; return events;
if (HasImage) if (HasImage)
return events = Module.Read (this, (type, reader) => reader.ReadEvents (type)); return Module.Read (ref events, this, (type, reader) => reader.ReadEvents (type));
return events = new MemberDefinitionCollection<EventDefinition> (this); return events = new MemberDefinitionCollection<EventDefinition> (this);
} }
@ -255,7 +255,7 @@ namespace Mono.Cecil {
return properties; return properties;
if (HasImage) if (HasImage)
return properties = Module.Read (this, (type, reader) => reader.ReadProperties (type)); return Module.Read (ref properties, this, (type, reader) => reader.ReadProperties (type));
return properties = new MemberDefinitionCollection<PropertyDefinition> (this); return properties = new MemberDefinitionCollection<PropertyDefinition> (this);
} }
@ -271,7 +271,7 @@ namespace Mono.Cecil {
} }
public Collection<SecurityDeclaration> SecurityDeclarations { public Collection<SecurityDeclaration> SecurityDeclarations {
get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (Module)); } get { return security_declarations ?? (this.GetSecurityDeclarations (ref security_declarations, Module)); }
} }
public bool HasCustomAttributes { public bool HasCustomAttributes {
@ -284,7 +284,7 @@ namespace Mono.Cecil {
} }
public Collection<CustomAttribute> CustomAttributes { public Collection<CustomAttribute> CustomAttributes {
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); } get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
} }
public override bool HasGenericParameters { public override bool HasGenericParameters {
@ -297,7 +297,7 @@ namespace Mono.Cecil {
} }
public override Collection<GenericParameter> GenericParameters { public override Collection<GenericParameter> GenericParameters {
get { return generic_parameters ?? (generic_parameters = this.GetGenericParameters (Module)); } get { return generic_parameters ?? (this.GetGenericParameters (ref generic_parameters, Module)); }
} }
#region TypeAttributes #region TypeAttributes

61
Mono.Cecil/Mono.Cecil/TypeSystem.cs

@ -174,18 +174,27 @@ namespace Mono.Cecil {
internal abstract TypeReference LookupType (string @namespace, string name); internal abstract TypeReference LookupType (string @namespace, string name);
TypeReference LookupSystemType (string name, ElementType element_type) TypeReference LookupSystemType (ref TypeReference typeRef, string name, ElementType element_type)
{ {
var type = LookupType ("System", name); lock (module.SyncRoot) {
type.etype = element_type; if (typeRef != null)
return type; return typeRef;
var type = LookupType ("System", name);
type.etype = element_type;
return typeRef = type;
}
} }
TypeReference LookupSystemValueType (string name, ElementType element_type) TypeReference LookupSystemValueType (ref TypeReference typeRef, string name, ElementType element_type)
{ {
var type = LookupSystemType (name, element_type); lock (module.SyncRoot) {
type.IsValueType = true; if (typeRef != null)
return type; return typeRef;
var type = LookupType ("System", name);
type.etype = element_type;
type.IsValueType = true;
return typeRef = type;
}
} }
public IMetadataScope Corlib { public IMetadataScope Corlib {
@ -199,75 +208,75 @@ namespace Mono.Cecil {
} }
public TypeReference Object { public TypeReference Object {
get { return type_object ?? (type_object = LookupSystemType ("Object", ElementType.Object)); } get { return type_object ?? (LookupSystemType (ref type_object, "Object", ElementType.Object)); }
} }
public TypeReference Void { public TypeReference Void {
get { return type_void ?? (type_void = LookupSystemType ("Void", ElementType.Void)); } get { return type_void ?? (LookupSystemType (ref type_void, "Void", ElementType.Void)); }
} }
public TypeReference Boolean { public TypeReference Boolean {
get { return type_bool ?? (type_bool = LookupSystemValueType ("Boolean", ElementType.Boolean)); } get { return type_bool ?? (LookupSystemValueType (ref type_bool, "Boolean", ElementType.Boolean)); }
} }
public TypeReference Char { public TypeReference Char {
get { return type_char ?? (type_char = LookupSystemValueType ("Char", ElementType.Char)); } get { return type_char ?? (LookupSystemValueType (ref type_char, "Char", ElementType.Char)); }
} }
public TypeReference SByte { public TypeReference SByte {
get { return type_sbyte ?? (type_sbyte = LookupSystemValueType ("SByte", ElementType.I1)); } get { return type_sbyte ?? (LookupSystemValueType (ref type_sbyte, "SByte", ElementType.I1)); }
} }
public TypeReference Byte { public TypeReference Byte {
get { return type_byte ?? (type_byte = LookupSystemValueType ("Byte", ElementType.U1)); } get { return type_byte ?? (LookupSystemValueType (ref type_byte, "Byte", ElementType.U1)); }
} }
public TypeReference Int16 { public TypeReference Int16 {
get { return type_int16 ?? (type_int16 = LookupSystemValueType ("Int16", ElementType.I2)); } get { return type_int16 ?? (LookupSystemValueType (ref type_int16, "Int16", ElementType.I2)); }
} }
public TypeReference UInt16 { public TypeReference UInt16 {
get { return type_uint16 ?? (type_uint16 = LookupSystemValueType ("UInt16", ElementType.U2)); } get { return type_uint16 ?? (LookupSystemValueType (ref type_uint16, "UInt16", ElementType.U2)); }
} }
public TypeReference Int32 { public TypeReference Int32 {
get { return type_int32 ?? (type_int32 = LookupSystemValueType ("Int32", ElementType.I4)); } get { return type_int32 ?? (LookupSystemValueType (ref type_int32, "Int32", ElementType.I4)); }
} }
public TypeReference UInt32 { public TypeReference UInt32 {
get { return type_uint32 ?? (type_uint32 = LookupSystemValueType ("UInt32", ElementType.U4)); } get { return type_uint32 ?? (LookupSystemValueType (ref type_uint32, "UInt32", ElementType.U4)); }
} }
public TypeReference Int64 { public TypeReference Int64 {
get { return type_int64 ?? (type_int64 = LookupSystemValueType ("Int64", ElementType.I8)); } get { return type_int64 ?? (LookupSystemValueType (ref type_int64, "Int64", ElementType.I8)); }
} }
public TypeReference UInt64 { public TypeReference UInt64 {
get { return type_uint64 ?? (type_uint64 = LookupSystemValueType ("UInt64", ElementType.U8)); } get { return type_uint64 ?? (LookupSystemValueType (ref type_uint64, "UInt64", ElementType.U8)); }
} }
public TypeReference Single { public TypeReference Single {
get { return type_single ?? (type_single = LookupSystemValueType ("Single", ElementType.R4)); } get { return type_single ?? (LookupSystemValueType (ref type_single, "Single", ElementType.R4)); }
} }
public TypeReference Double { public TypeReference Double {
get { return type_double ?? (type_double = LookupSystemValueType ("Double", ElementType.R8)); } get { return type_double ?? (LookupSystemValueType (ref type_double, "Double", ElementType.R8)); }
} }
public TypeReference IntPtr { public TypeReference IntPtr {
get { return type_intptr ?? (type_intptr = LookupSystemValueType ("IntPtr", ElementType.I)); } get { return type_intptr ?? (LookupSystemValueType (ref type_intptr, "IntPtr", ElementType.I)); }
} }
public TypeReference UIntPtr { public TypeReference UIntPtr {
get { return type_uintptr ?? (type_uintptr = LookupSystemValueType ("UIntPtr", ElementType.U)); } get { return type_uintptr ?? (LookupSystemValueType (ref type_uintptr, "UIntPtr", ElementType.U)); }
} }
public TypeReference String { public TypeReference String {
get { return type_string ?? (type_string = LookupSystemType ("String", ElementType.String)); } get { return type_string ?? (LookupSystemType (ref type_string, "String", ElementType.String)); }
} }
public TypeReference TypedReference { public TypeReference TypedReference {
get { return type_typedref ?? (type_typedref = LookupSystemValueType ("TypedReference", ElementType.TypedByRef)); } get { return type_typedref ?? (LookupSystemValueType (ref type_typedref, "TypedReference", ElementType.TypedByRef)); }
} }
} }
} }

Loading…
Cancel
Save