PageRenderTime 17ms CodeModel.GetById 4ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/symbols/pdb/Mono.Cecil.Pdb/SymWriter.cs

http://github.com/jbevain/cecil
C# | 156 lines | 118 code | 29 blank | 9 comment | 2 complexity | 14cc0a3c16a915efa9dc11da5e7f7781 MD5 | raw file
  1//
  2// Author:
  3//   Juerg Billeter (j@bitron.ch)
  4//
  5// (C) 2008 Juerg Billeter
  6//
  7// Licensed under the MIT/X11 license.
  8//
  9
 10using System;
 11using System.Runtime.InteropServices;
 12
 13using Mono.Cecil.Cil;
 14using Mono.Collections.Generic;
 15
 16namespace Mono.Cecil.Pdb
 17{
 18	internal class SymWriter
 19	{
 20		[DllImport("ole32.dll")]
 21		static extern int CoCreateInstance (
 22			[In] ref Guid rclsid,
 23			[In, MarshalAs (UnmanagedType.IUnknown)] object pUnkOuter,
 24			[In] uint dwClsContext,
 25			[In] ref Guid riid,
 26			[Out, MarshalAs (UnmanagedType.Interface)] out object ppv);
 27
 28		static Guid s_symUnmangedWriterIID = new Guid("0b97726e-9e6d-4f05-9a26-424022093caa");
 29		static Guid s_CorSymWriter_SxS_ClassID = new Guid ("108296c1-281e-11d3-bd22-0000f80849bd");
 30
 31		readonly ISymUnmanagedWriter2 writer;
 32		readonly Collection<ISymUnmanagedDocumentWriter> documents;
 33
 34		public SymWriter ()
 35		{
 36			object objWriter;
 37			CoCreateInstance (ref s_CorSymWriter_SxS_ClassID, null, 1, ref s_symUnmangedWriterIID, out objWriter);
 38
 39			writer = (ISymUnmanagedWriter2) objWriter;
 40			documents = new Collection<ISymUnmanagedDocumentWriter> ();
 41		}
 42
 43		public byte[] GetDebugInfo (out ImageDebugDirectory idd)
 44		{
 45			int size;
 46
 47			// get size of debug info
 48			writer.GetDebugInfo (out idd, 0, out size, null);
 49
 50			byte[] debug_info = new byte[size];
 51			writer.GetDebugInfo (out idd, size, out size, debug_info);
 52
 53			return debug_info;
 54		}
 55
 56		public void DefineLocalVariable2 (
 57			string name,
 58			VariableAttributes attributes,
 59			int sigToken,
 60			int addr1,
 61			int addr2,
 62			int addr3,
 63			int startOffset,
 64			int endOffset)
 65		{
 66			writer.DefineLocalVariable2 (name, (int)attributes, sigToken, 1 /* ILOffset*/, addr1, addr2, addr3, startOffset, endOffset);
 67		}
 68
 69		public void DefineConstant2 (string name, object value, int sigToken)
 70		{
 71			if (value == null) {
 72				writer.DefineConstant2 (name, 0, sigToken);
 73				return;
 74			}
 75
 76			writer.DefineConstant2 (name, value, sigToken);
 77		}
 78
 79		public void Close ()
 80		{
 81			writer.Close ();
 82			Marshal.ReleaseComObject (writer);
 83
 84			foreach (var document in documents)
 85				Marshal.ReleaseComObject (document);
 86		}
 87
 88		public void CloseMethod ()
 89		{
 90			writer.CloseMethod ();
 91		}
 92
 93		public void CloseNamespace ()
 94		{
 95			writer.CloseNamespace ();
 96		}
 97
 98		public void CloseScope (int endOffset)
 99		{
100			writer.CloseScope (endOffset);
101		}
102
103		public SymDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType)
104		{
105			ISymUnmanagedDocumentWriter doc_writer;
106			writer.DefineDocument (url, ref language, ref languageVendor, ref documentType, out doc_writer);
107
108			documents.Add (doc_writer);
109			return new SymDocumentWriter (doc_writer);
110		}
111
112		public void DefineSequencePoints (SymDocumentWriter document, int[] offsets, int[] lines, int[] columns, int[] endLines, int[] endColumns)
113		{
114			writer.DefineSequencePoints (document.Writer, offsets.Length, offsets, lines, columns, endLines, endColumns);
115		}
116
117		public void Initialize (object emitter, string filename, bool fFullBuild)
118		{
119			writer.Initialize (emitter, filename, null, fFullBuild);
120		}
121
122		public void SetUserEntryPoint (int methodToken)
123		{
124			writer.SetUserEntryPoint (methodToken);
125		}
126
127		public void OpenMethod (int methodToken)
128		{
129			writer.OpenMethod (methodToken);
130		}
131
132		public void OpenNamespace (string name)
133		{
134			writer.OpenNamespace (name);
135		}
136
137		public int OpenScope (int startOffset)
138		{
139			int result;
140			writer.OpenScope (startOffset, out result);
141			return result;
142		}
143
144		public void UsingNamespace (string fullName)
145		{
146			writer.UsingNamespace (fullName);
147		}
148
149		public void DefineCustomMetadata (string name, byte [] metadata)
150		{
151			var handle = GCHandle.Alloc (metadata, GCHandleType.Pinned);
152			writer.SetSymAttribute (0, name, (uint) metadata.Length, handle.AddrOfPinnedObject ());
153			handle.Free ();
154		}
155	}
156}