2010-03-05 Rodrigo Kumpera <rkumpera@novell.com>
[mcs.git] / mcs / symbolwriter.cs
blobacdaff64e7c6b286082178617bebf8183885ffd9
1 //
2 // symbolwriter.cs: The symbol writer
3 //
4 // Author:
5 // Martin Baulig (martin@ximian.com)
6 //
7 // Copyright 2003 Ximian, Inc.
8 // Copyright 2003-2008 Novell, Inc.
9 //
11 using System;
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 {
27 #if !NET_4_0
28 delegate int GetILOffsetFunc (ILGenerator ig);
29 GetILOffsetFunc get_il_offset_func;
30 #endif
31 delegate Guid GetGuidFunc (ModuleBuilder mb);
33 GetGuidFunc get_guid_func;
35 ModuleBuilder module_builder;
37 public SymbolWriterImpl (ModuleBuilder module_builder, string filename)
38 : base (filename)
40 this.module_builder = module_builder;
43 public int GetILOffset (ILGenerator ig)
45 #if NET_4_0
46 return ig.ILOffset;
47 #else
48 return get_il_offset_func (ig);
49 #endif
52 public void WriteSymbolFile ()
54 Guid guid = get_guid_func (module_builder);
55 WriteSymbolFile (guid);
58 public bool Initialize ()
60 MethodInfo mi;
61 #if !NET_4_0
62 mi = typeof (ILGenerator).GetMethod (
63 "Mono_GetCurrentOffset",
64 BindingFlags.Static | BindingFlags.NonPublic);
65 if (mi == null)
66 return false;
68 get_il_offset_func = (GetILOffsetFunc) System.Delegate.CreateDelegate (
69 typeof (GetILOffsetFunc), mi);
70 #endif
72 mi = typeof (ModuleBuilder).GetMethod (
73 "Mono_GetGuid",
74 BindingFlags.Static | BindingFlags.NonPublic);
75 if (mi == null)
76 return false;
78 get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
79 typeof (GetGuidFunc), mi);
81 Location.DefineSymbolDocuments (this);
83 return true;
87 public static void DefineLocalVariable (string name, LocalBuilder builder)
89 if (symwriter != null) {
90 int index = MonoDebuggerSupport.GetLocalIndex (builder);
91 symwriter.DefineLocalVariable (index, name);
95 public static SourceMethodBuilder OpenMethod (ICompileUnit file, int ns_id,
96 IMethodDef method)
98 if (symwriter != null)
99 return symwriter.OpenMethod (file, ns_id, method);
100 else
101 return null;
104 public static void CloseMethod ()
106 if (symwriter != null)
107 symwriter.CloseMethod ();
110 public static int OpenScope (ILGenerator ig)
112 if (symwriter != null) {
113 int offset = symwriter.GetILOffset (ig);
114 return symwriter.OpenScope (offset);
115 } else {
116 return -1;
120 public static void CloseScope (ILGenerator ig)
122 if (symwriter != null) {
123 int offset = symwriter.GetILOffset (ig);
124 symwriter.CloseScope (offset);
128 public static int DefineNamespace (string name, CompileUnitEntry source,
129 string[] using_clauses, int parent)
131 if (symwriter != null)
132 return symwriter.DefineNamespace (name, source, using_clauses, parent);
133 else
134 return -1;
137 #region Terrania additions
138 public static void DefineAnonymousScope (int id)
140 if (symwriter != null)
141 symwriter.DefineAnonymousScope (id);
144 public static void DefineScopeVariable (int scope, LocalBuilder builder)
146 if (symwriter != null) {
147 int index = MonoDebuggerSupport.GetLocalIndex (builder);
148 symwriter.DefineScopeVariable (scope, index);
152 public static void DefineScopeVariable (int scope)
154 if (symwriter != null)
155 symwriter.DefineScopeVariable (scope, -1);
158 public static void DefineCapturedLocal (int scope_id, string name,
159 string captured_name)
161 if (symwriter != null)
162 symwriter.DefineCapturedLocal (scope_id, name, captured_name);
165 public static void DefineCapturedParameter (int scope_id, string name,
166 string captured_name)
168 if (symwriter != null)
169 symwriter.DefineCapturedParameter (scope_id, name, captured_name);
172 public static void DefineCapturedThis (int scope_id, string captured_name)
174 if (symwriter != null)
175 symwriter.DefineCapturedThis (scope_id, captured_name);
178 public static void DefineCapturedScope (int scope_id, int id, string captured_name)
180 if (symwriter != null)
181 symwriter.DefineCapturedScope (scope_id, id, captured_name);
184 public static void OpenCompilerGeneratedBlock (ILGenerator ig)
186 if (symwriter != null) {
187 int offset = symwriter.GetILOffset (ig);
188 symwriter.OpenCompilerGeneratedBlock (offset);
192 public static void CloseCompilerGeneratedBlock (ILGenerator ig)
194 if (symwriter != null) {
195 int offset = symwriter.GetILOffset (ig);
196 symwriter.CloseCompilerGeneratedBlock (offset);
200 public static void StartIteratorBody (ILGenerator ig)
202 if (symwriter != null) {
203 int offset = symwriter.GetILOffset (ig);
204 symwriter.StartIteratorBody (offset);
208 public static void EndIteratorBody (ILGenerator ig)
210 if (symwriter != null) {
211 int offset = symwriter.GetILOffset (ig);
212 symwriter.EndIteratorBody (offset);
216 public static void StartIteratorDispatcher (ILGenerator ig)
218 if (symwriter != null) {
219 int offset = symwriter.GetILOffset (ig);
220 symwriter.StartIteratorDispatcher (offset);
224 public static void EndIteratorDispatcher (ILGenerator ig)
226 if (symwriter != null) {
227 int offset = symwriter.GetILOffset (ig);
228 symwriter.EndIteratorDispatcher (offset);
231 #endregion
233 public static void MarkSequencePoint (ILGenerator ig, Location loc)
235 if (symwriter != null) {
236 SourceFileEntry file = loc.SourceFile.SourceFileEntry;
237 int offset = symwriter.GetILOffset (ig);
238 symwriter.MarkSequencePoint (
239 offset, file, loc.Row, loc.Column, loc.Hidden);
243 public static void WriteSymbolFile ()
245 if (symwriter != null)
246 symwriter.WriteSymbolFile ();
249 public static bool Initialize (ModuleBuilder module, string filename)
251 symwriter = new SymbolWriterImpl (module, filename);
252 if (!symwriter.Initialize ()) {
253 symwriter = null;
254 return false;
257 return true;
260 public static void Reset ()
262 symwriter = null;