diff options
author | Martin Baulig <martin.baulig@xamarin.com> | 2013-09-25 00:42:24 +0400 |
---|---|---|
committer | Martin Baulig <martin.baulig@xamarin.com> | 2013-09-25 00:42:24 +0400 |
commit | a868b2c94e39c958e01eb5f55323cfeda031f946 (patch) | |
tree | 6ca5125e735e3f55f9e4fc1d3665acff9a5c6d29 | |
parent | 89cfb1d4079002394010fd351628ad057c94422f (diff) |
Add some new internal APIs to remap types.
* Import.cs (MetadataImporter):
- Make `ImportType(TypeReference,ImportGenericContext)` virtual.
- Make `ImportAssemblyName(AssemblyNameReference)' protected virtual.
* ModuleDefinition.cs:
- Add new internal `ICustomMetadataWriter` interface.
- Add `ModuleDefinition.SetMetadataImporter' to provide a custom
importer (subclass of `MetadataImporter' which may use the new
virtuals to override).
- Add `ModuleDefinition.CustomMetadataWriter` property to provide
a `ICustomMetadataWriter' to remap TypeRef tokens.
* AssemblyWriter.cs:
- MetadataBuilder.GetTypeRefToken(): Use the new optional
`ICustomMetadataWriter' to allow remapping of TypeReference,
an implementation may choose to either replace TypeReference
with a different one or provide a custom MetadataToken.
All the newly added APIs are internal and may change in future.
They are used in an internal Xamarin project, which patches the
build to add `[assembly: InternalsVisibleTo]'.
-rw-r--r-- | Mono.Cecil/AssemblyWriter.cs | 7 | ||||
-rw-r--r-- | Mono.Cecil/Import.cs | 4 | ||||
-rw-r--r-- | Mono.Cecil/ModuleDefinition.cs | 29 |
3 files changed, 37 insertions, 3 deletions
diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index 0597bc0..f1026ee 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -1164,9 +1164,14 @@ namespace Mono.Cecil { MetadataToken GetTypeRefToken (TypeReference type) { + MetadataToken token; + if (module.CustomMetadataWriter != null) { + if (module.CustomMetadataWriter.CreateTypeRefToken (ref type, out token)) + return token; + } + var row = CreateTypeRefRow (type); - MetadataToken token; if (type_ref_map.TryGetValue (row, out token)) return token; diff --git a/Mono.Cecil/Import.cs b/Mono.Cecil/Import.cs index a32eb3c..dc9312b 100644 --- a/Mono.Cecil/Import.cs +++ b/Mono.Cecil/Import.cs @@ -437,7 +437,7 @@ namespace Mono.Cecil { } #endif - public TypeReference ImportType (TypeReference type, ImportGenericContext context) + public virtual TypeReference ImportType (TypeReference type, ImportGenericContext context) { if (type.IsTypeSpecification ()) return ImportTypeSpecification (type, context); @@ -474,7 +474,7 @@ namespace Mono.Cecil { throw new NotSupportedException (); } - AssemblyNameReference ImportAssemblyName (AssemblyNameReference name) + protected virtual AssemblyNameReference ImportAssemblyName (AssemblyNameReference name) { AssemblyNameReference reference; if (TryGetAssemblyNameReference (name, out reference)) diff --git a/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/ModuleDefinition.cs index 7f75cbd..61870cd 100644 --- a/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/ModuleDefinition.cs @@ -157,6 +157,22 @@ namespace Mono.Cecil { } } + interface ICustomMetadataWriter + { + /* + * Remap TypeReference or create custom TypeRef token. + * + * Return true to use the returned custom 'token'. + * + * Return false to create a TypeRef token for 'type' + * (which may have been replaced with a different TypeReference). + * + * This is necessary when types are moved from one assembly to another + * to either adjust the scope or replace a TypeRef with a TypeDef token. + */ + bool CreateTypeRefToken (ref TypeReference type, out MetadataToken token); + } + public sealed class WriterParameters { Stream symbol_stream; @@ -216,6 +232,7 @@ namespace Mono.Cecil { #if !READ_ONLY MetadataImporter importer; + ICustomMetadataWriter custom_writer; #endif Collection<CustomAttribute> custom_attributes; Collection<AssemblyNameReference> references; @@ -286,6 +303,18 @@ namespace Mono.Cecil { internal MetadataImporter MetadataImporter { get { return importer ?? (importer = new MetadataImporter (this)); } } + + internal void SetMetadataImporter (MetadataImporter importer) + { + if (this.importer != null) + throw new InvalidOperationException (); + this.importer = importer; + } + + internal ICustomMetadataWriter CustomMetadataWriter { + get { return custom_writer; } + set { custom_writer = value; } + } #endif public IAssemblyResolver AssemblyResolver { |