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.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
16 using Mono.CompilerServices.SymbolWriter;
18 namespace Mono.CSharp {
19 public static class SymbolWriter
21 public static bool HasSymbolWriter {
22 get { return symwriter != null; }
25 private static SymbolWriterImpl symwriter;
27 class SymbolWriterImpl : MonoSymbolWriter {
29 delegate int GetILOffsetFunc (ILGenerator ig);
30 GetILOffsetFunc get_il_offset_func;
32 delegate Guid GetGuidFunc (ModuleBuilder mb);
34 GetGuidFunc get_guid_func;
36 ModuleBuilder module_builder;
38 public SymbolWriterImpl (ModuleBuilder module_builder, string filename)
41 this.module_builder = module_builder;
44 public int GetILOffset (ILGenerator ig)
49 return get_il_offset_func (ig);
53 public void WriteSymbolFile ()
55 Guid guid = get_guid_func (module_builder);
56 WriteSymbolFile (guid);
59 public bool Initialize ()
63 mi = typeof (ILGenerator).GetMethod (
64 "Mono_GetCurrentOffset",
65 BindingFlags.Static | BindingFlags.NonPublic);
69 get_il_offset_func = (GetILOffsetFunc) System.Delegate.CreateDelegate (
70 typeof (GetILOffsetFunc), mi);
73 mi = typeof (ModuleBuilder).GetMethod (
75 BindingFlags.Static | BindingFlags.NonPublic);
79 get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
80 typeof (GetGuidFunc), mi);
82 Location.DefineSymbolDocuments (this);
88 public static void DefineLocalVariable (string name, LocalBuilder builder)
90 if (symwriter != null) {
91 int index = MonoDebuggerSupport.GetLocalIndex (builder);
92 symwriter.DefineLocalVariable (index, name);
96 public static SourceMethodBuilder OpenMethod (ICompileUnit file, int ns_id,
99 if (symwriter != null)
100 return symwriter.OpenMethod (file, ns_id, method);
105 public static void CloseMethod ()
107 if (symwriter != null)
108 symwriter.CloseMethod ();
111 public static int OpenScope (ILGenerator ig)
113 if (symwriter != null) {
114 int offset = symwriter.GetILOffset (ig);
115 return symwriter.OpenScope (offset);
121 public static void CloseScope (ILGenerator ig)
123 if (symwriter != null) {
124 int offset = symwriter.GetILOffset (ig);
125 symwriter.CloseScope (offset);
129 public static int DefineNamespace (string name, CompileUnitEntry source,
130 string[] using_clauses, int parent)
132 if (symwriter != null)
133 return symwriter.DefineNamespace (name, source, using_clauses, parent);
138 #region Terrania additions
139 public static void DefineAnonymousScope (int id)
141 if (symwriter != null)
142 symwriter.DefineAnonymousScope (id);
145 public static void DefineScopeVariable (int scope, LocalBuilder builder)
147 if (symwriter != null) {
148 int index = MonoDebuggerSupport.GetLocalIndex (builder);
149 symwriter.DefineScopeVariable (scope, index);
153 public static void DefineScopeVariable (int scope)
155 if (symwriter != null)
156 symwriter.DefineScopeVariable (scope, -1);
159 public static void DefineCapturedLocal (int scope_id, string name,
160 string captured_name)
162 if (symwriter != null)
163 symwriter.DefineCapturedLocal (scope_id, name, captured_name);
166 public static void DefineCapturedParameter (int scope_id, string name,
167 string captured_name)
169 if (symwriter != null)
170 symwriter.DefineCapturedParameter (scope_id, name, captured_name);
173 public static void DefineCapturedThis (int scope_id, string captured_name)
175 if (symwriter != null)
176 symwriter.DefineCapturedThis (scope_id, captured_name);
179 public static void DefineCapturedScope (int scope_id, int id, string captured_name)
181 if (symwriter != null)
182 symwriter.DefineCapturedScope (scope_id, id, captured_name);
185 public static void OpenCompilerGeneratedBlock (ILGenerator ig)
187 if (symwriter != null) {
188 int offset = symwriter.GetILOffset (ig);
189 symwriter.OpenCompilerGeneratedBlock (offset);
193 public static void CloseCompilerGeneratedBlock (ILGenerator ig)
195 if (symwriter != null) {
196 int offset = symwriter.GetILOffset (ig);
197 symwriter.CloseCompilerGeneratedBlock (offset);
201 public static void StartIteratorBody (ILGenerator ig)
203 if (symwriter != null) {
204 int offset = symwriter.GetILOffset (ig);
205 symwriter.StartIteratorBody (offset);
209 public static void EndIteratorBody (ILGenerator ig)
211 if (symwriter != null) {
212 int offset = symwriter.GetILOffset (ig);
213 symwriter.EndIteratorBody (offset);
217 public static void StartIteratorDispatcher (ILGenerator ig)
219 if (symwriter != null) {
220 int offset = symwriter.GetILOffset (ig);
221 symwriter.StartIteratorDispatcher (offset);
225 public static void EndIteratorDispatcher (ILGenerator ig)
227 if (symwriter != null) {
228 int offset = symwriter.GetILOffset (ig);
229 symwriter.EndIteratorDispatcher (offset);
234 public static void MarkSequencePoint (ILGenerator ig, Location loc)
236 if (symwriter != null) {
237 SourceFileEntry file = loc.SourceFile.SourceFileEntry;
238 int offset = symwriter.GetILOffset (ig);
239 symwriter.MarkSequencePoint (
240 offset, file, loc.Row, loc.Column, loc.Hidden);
244 public static void WriteSymbolFile ()
246 if (symwriter != null)
247 symwriter.WriteSymbolFile ();
250 public static bool Initialize (ModuleBuilder module, string filename)
252 symwriter = new SymbolWriterImpl (module, filename);
253 if (!symwriter.Initialize ()) {
261 public static void Reset ()