2010-01-12 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / mcs / symbolwriter.cs
blob53462122c37840a10d734fc8f3a2b8c56c1c7e75
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 delegate int GetILOffsetFunc (ILGenerator ig);
29 delegate Guid GetGuidFunc (ModuleBuilder mb);
31 GetILOffsetFunc get_il_offset_func;
32 GetGuidFunc get_guid_func;
34 ModuleBuilder module_builder;
36 public SymbolWriterImpl (ModuleBuilder module_builder, string filename)
37 : base (filename)
39 this.module_builder = module_builder;
42 public int GetILOffset (ILGenerator ig)
44 return get_il_offset_func (ig);
47 public void WriteSymbolFile ()
49 Guid guid = get_guid_func (module_builder);
50 WriteSymbolFile (guid);
53 public bool Initialize ()
55 MethodInfo mi = typeof (ILGenerator).GetMethod (
56 "Mono_GetCurrentOffset",
57 BindingFlags.Static | BindingFlags.NonPublic);
58 if (mi == null)
59 return false;
61 get_il_offset_func = (GetILOffsetFunc) System.Delegate.CreateDelegate (
62 typeof (GetILOffsetFunc), mi);
64 mi = typeof (ModuleBuilder).GetMethod (
65 "Mono_GetGuid",
66 BindingFlags.Static | BindingFlags.NonPublic);
67 if (mi == null)
68 return false;
70 get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
71 typeof (GetGuidFunc), mi);
73 Location.DefineSymbolDocuments (this);
75 return true;
79 public static void DefineLocalVariable (string name, LocalBuilder builder)
81 if (symwriter != null) {
82 int index = MonoDebuggerSupport.GetLocalIndex (builder);
83 symwriter.DefineLocalVariable (index, name);
87 public static SourceMethodBuilder OpenMethod (ICompileUnit file, int ns_id,
88 IMethodDef method)
90 if (symwriter != null)
91 return symwriter.OpenMethod (file, ns_id, method);
92 else
93 return null;
96 public static void CloseMethod ()
98 if (symwriter != null)
99 symwriter.CloseMethod ();
102 public static int OpenScope (ILGenerator ig)
104 if (symwriter != null) {
105 int offset = symwriter.GetILOffset (ig);
106 return symwriter.OpenScope (offset);
107 } else {
108 return -1;
112 public static void CloseScope (ILGenerator ig)
114 if (symwriter != null) {
115 int offset = symwriter.GetILOffset (ig);
116 symwriter.CloseScope (offset);
120 public static int DefineNamespace (string name, CompileUnitEntry source,
121 string[] using_clauses, int parent)
123 if (symwriter != null)
124 return symwriter.DefineNamespace (name, source, using_clauses, parent);
125 else
126 return -1;
129 #region Terrania additions
130 public static void DefineAnonymousScope (int id)
132 if (symwriter != null)
133 symwriter.DefineAnonymousScope (id);
136 public static void DefineScopeVariable (int scope, LocalBuilder builder)
138 if (symwriter != null) {
139 int index = MonoDebuggerSupport.GetLocalIndex (builder);
140 symwriter.DefineScopeVariable (scope, index);
144 public static void DefineScopeVariable (int scope)
146 if (symwriter != null)
147 symwriter.DefineScopeVariable (scope, -1);
150 public static void DefineCapturedLocal (int scope_id, string name,
151 string captured_name)
153 if (symwriter != null)
154 symwriter.DefineCapturedLocal (scope_id, name, captured_name);
157 public static void DefineCapturedParameter (int scope_id, string name,
158 string captured_name)
160 if (symwriter != null)
161 symwriter.DefineCapturedParameter (scope_id, name, captured_name);
164 public static void DefineCapturedThis (int scope_id, string captured_name)
166 if (symwriter != null)
167 symwriter.DefineCapturedThis (scope_id, captured_name);
170 public static void DefineCapturedScope (int scope_id, int id, string captured_name)
172 if (symwriter != null)
173 symwriter.DefineCapturedScope (scope_id, id, captured_name);
176 public static void OpenCompilerGeneratedBlock (ILGenerator ig)
178 if (symwriter != null) {
179 int offset = symwriter.GetILOffset (ig);
180 symwriter.OpenCompilerGeneratedBlock (offset);
184 public static void CloseCompilerGeneratedBlock (ILGenerator ig)
186 if (symwriter != null) {
187 int offset = symwriter.GetILOffset (ig);
188 symwriter.CloseCompilerGeneratedBlock (offset);
192 public static void StartIteratorBody (ILGenerator ig)
194 if (symwriter != null) {
195 int offset = symwriter.GetILOffset (ig);
196 symwriter.StartIteratorBody (offset);
200 public static void EndIteratorBody (ILGenerator ig)
202 if (symwriter != null) {
203 int offset = symwriter.GetILOffset (ig);
204 symwriter.EndIteratorBody (offset);
208 public static void StartIteratorDispatcher (ILGenerator ig)
210 if (symwriter != null) {
211 int offset = symwriter.GetILOffset (ig);
212 symwriter.StartIteratorDispatcher (offset);
216 public static void EndIteratorDispatcher (ILGenerator ig)
218 if (symwriter != null) {
219 int offset = symwriter.GetILOffset (ig);
220 symwriter.EndIteratorDispatcher (offset);
223 #endregion
225 public static void MarkSequencePoint (ILGenerator ig, Location loc)
227 if (symwriter != null) {
228 SourceFileEntry file = loc.SourceFile.SourceFileEntry;
229 int offset = symwriter.GetILOffset (ig);
230 symwriter.MarkSequencePoint (
231 offset, file, loc.Row, loc.Column, loc.Hidden);
235 public static void WriteSymbolFile ()
237 if (symwriter != null)
238 symwriter.WriteSymbolFile ();
241 public static bool Initialize (ModuleBuilder module, string filename)
243 symwriter = new SymbolWriterImpl (module, filename);
244 if (!symwriter.Initialize ()) {
245 symwriter = null;
246 return false;
249 return true;
252 public static void Reset ()
254 symwriter = null;