2 // symbolwriter.cs: The symbol writer
5 // Martin Baulig (martin@ximian.com)
7 // Copyright 2003 Ximian, Inc.
8 // Copyright 2003-2008 Novell, Inc.
12 using System.Reflection;
13 using System.Reflection.Emit;
15 using Mono.CompilerServices.SymbolWriter;
17 namespace Mono.CSharp {
18 public static class SymbolWriter
20 public static bool HasSymbolWriter {
21 get { return symwriter != null; }
24 private static SymbolWriterImpl symwriter;
26 class SymbolWriterImpl : MonoSymbolWriter {
28 delegate int GetILOffsetFunc (ILGenerator ig);
29 GetILOffsetFunc get_il_offset_func;
31 delegate Guid GetGuidFunc (ModuleBuilder mb);
32 GetGuidFunc get_guid_func;
35 ModuleBuilder module_builder;
37 public SymbolWriterImpl (ModuleBuilder module_builder, string filename)
40 this.module_builder = module_builder;
43 public int GetILOffset (ILGenerator ig)
48 return get_il_offset_func (ig);
52 public void WriteSymbolFile ()
56 guid = module_builder.ModuleVersionId;
58 guid = get_guid_func (module_builder);
60 WriteSymbolFile (guid);
63 public bool Initialize ()
68 mi = typeof (ILGenerator).GetMethod (
69 "Mono_GetCurrentOffset",
70 BindingFlags.Static | BindingFlags.NonPublic);
74 get_il_offset_func = (GetILOffsetFunc) System.Delegate.CreateDelegate (
75 typeof (GetILOffsetFunc), mi);
77 mi = typeof (ModuleBuilder).GetMethod (
79 BindingFlags.Static | BindingFlags.NonPublic);
83 get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
84 typeof (GetGuidFunc), mi);
87 Location.DefineSymbolDocuments (this);
92 public static void DefineLocalVariable (string name, LocalBuilder builder)
94 if (symwriter != null) {
95 int index = MonoDebuggerSupport.GetLocalIndex (builder);
96 symwriter.DefineLocalVariable (index, name);
100 public static SourceMethodBuilder OpenMethod (ICompileUnit file, int ns_id,
103 if (symwriter != null)
104 return symwriter.OpenMethod (file, ns_id, method);
109 public static void CloseMethod ()
111 if (symwriter != null)
112 symwriter.CloseMethod ();
115 public static int OpenScope (ILGenerator ig)
117 if (symwriter != null) {
118 int offset = symwriter.GetILOffset (ig);
119 return symwriter.OpenScope (offset);
125 public static void CloseScope (ILGenerator ig)
127 if (symwriter != null) {
128 int offset = symwriter.GetILOffset (ig);
129 symwriter.CloseScope (offset);
133 public static int DefineNamespace (string name, CompileUnitEntry source,
134 string[] using_clauses, int parent)
136 if (symwriter != null)
137 return symwriter.DefineNamespace (name, source, using_clauses, parent);
142 #region Terrania additions
143 public static void DefineAnonymousScope (int id)
145 if (symwriter != null)
146 symwriter.DefineAnonymousScope (id);
149 public static void DefineScopeVariable (int scope, LocalBuilder builder)
151 if (symwriter != null) {
152 int index = MonoDebuggerSupport.GetLocalIndex (builder);
153 symwriter.DefineScopeVariable (scope, index);
157 public static void DefineScopeVariable (int scope)
159 if (symwriter != null)
160 symwriter.DefineScopeVariable (scope, -1);
163 public static void DefineCapturedLocal (int scope_id, string name,
164 string captured_name)
166 if (symwriter != null)
167 symwriter.DefineCapturedLocal (scope_id, name, captured_name);
170 public static void DefineCapturedParameter (int scope_id, string name,
171 string captured_name)
173 if (symwriter != null)
174 symwriter.DefineCapturedParameter (scope_id, name, captured_name);
177 public static void DefineCapturedThis (int scope_id, string captured_name)
179 if (symwriter != null)
180 symwriter.DefineCapturedThis (scope_id, captured_name);
183 public static void DefineCapturedScope (int scope_id, int id, string captured_name)
185 if (symwriter != null)
186 symwriter.DefineCapturedScope (scope_id, id, captured_name);
189 public static void OpenCompilerGeneratedBlock (EmitContext ec)
191 if (symwriter != null) {
192 int offset = symwriter.GetILOffset (ec.ig);
193 symwriter.OpenCompilerGeneratedBlock (offset);
197 public static void CloseCompilerGeneratedBlock (EmitContext ec)
199 if (symwriter != null) {
200 int offset = symwriter.GetILOffset (ec.ig);
201 symwriter.CloseCompilerGeneratedBlock (offset);
205 public static void StartIteratorBody (EmitContext ec)
207 if (symwriter != null) {
208 int offset = symwriter.GetILOffset (ec.ig);
209 symwriter.StartIteratorBody (offset);
213 public static void EndIteratorBody (EmitContext ec)
215 if (symwriter != null) {
216 int offset = symwriter.GetILOffset (ec.ig);
217 symwriter.EndIteratorBody (offset);
221 public static void StartIteratorDispatcher (EmitContext ec)
223 if (symwriter != null) {
224 int offset = symwriter.GetILOffset (ec.ig);
225 symwriter.StartIteratorDispatcher (offset);
229 public static void EndIteratorDispatcher (EmitContext ec)
231 if (symwriter != null) {
232 int offset = symwriter.GetILOffset (ec.ig);
233 symwriter.EndIteratorDispatcher (offset);
238 public static void MarkSequencePoint (ILGenerator ig, Location loc)
240 if (symwriter != null) {
241 SourceFileEntry file = loc.SourceFile.SourceFileEntry;
242 int offset = symwriter.GetILOffset (ig);
243 symwriter.MarkSequencePoint (
244 offset, file, loc.Row, loc.Column, loc.Hidden);
248 public static void WriteSymbolFile ()
250 if (symwriter != null)
251 symwriter.WriteSymbolFile ();
254 public static bool Initialize (ModuleBuilder module, string filename)
256 symwriter = new SymbolWriterImpl (module, filename);
257 if (!symwriter.Initialize ()) {
265 public static void Reset ()