2009-11-24 Jb Evain <jbevain@novell.com>
[mcs.git] / mcs / symbolwriter.cs
blob12fa163d966a79711e7343bd2d1bcfaf787258e3
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.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 {
28 #if !NET_4_0
29 delegate int GetILOffsetFunc (ILGenerator ig);
30 GetILOffsetFunc get_il_offset_func;
31 #endif
32 delegate Guid GetGuidFunc (ModuleBuilder mb);
34 GetGuidFunc get_guid_func;
36 ModuleBuilder module_builder;
38 public SymbolWriterImpl (ModuleBuilder module_builder, string filename)
39 : base (filename)
41 this.module_builder = module_builder;
44 public int GetILOffset (ILGenerator ig)
46 #if NET_4_0
47 return ig.ILOffset;
48 #else
49 return get_il_offset_func (ig);
50 #endif
53 public void WriteSymbolFile ()
55 Guid guid = get_guid_func (module_builder);
56 WriteSymbolFile (guid);
59 public bool Initialize ()
61 MethodInfo mi;
62 #if !NET_4_0
63 mi = typeof (ILGenerator).GetMethod (
64 "Mono_GetCurrentOffset",
65 BindingFlags.Static | BindingFlags.NonPublic);
66 if (mi == null)
67 return false;
69 get_il_offset_func = (GetILOffsetFunc) System.Delegate.CreateDelegate (
70 typeof (GetILOffsetFunc), mi);
71 #endif
73 mi = typeof (ModuleBuilder).GetMethod (
74 "Mono_GetGuid",
75 BindingFlags.Static | BindingFlags.NonPublic);
76 if (mi == null)
77 return false;
79 get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
80 typeof (GetGuidFunc), mi);
82 Location.DefineSymbolDocuments (this);
84 return true;
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,
97 IMethodDef method)
99 if (symwriter != null)
100 return symwriter.OpenMethod (file, ns_id, method);
101 else
102 return null;
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);
116 } else {
117 return -1;
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);
134 else
135 return -1;
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);
232 #endregion
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 ()) {
254 symwriter = null;
255 return false;
258 return true;
261 public static void Reset ()
263 symwriter = null;