768d35538e934e01770b1dee052b7b774dcf908f
[mono.git] / mcs / mcs / symbolwriter.cs
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 //
10
11 using System;
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
15
16 using Mono.CompilerServices.SymbolWriter;
17
18 namespace Mono.CSharp {
19         public static class SymbolWriter
20         {
21                 public static bool HasSymbolWriter {
22                         get { return symwriter != null; }
23                 }
24
25                 private static SymbolWriterImpl symwriter;
26
27                 class SymbolWriterImpl : MonoSymbolWriter {
28                         delegate int GetILOffsetFunc (ILGenerator ig);
29                         delegate Guid GetGuidFunc (ModuleBuilder mb);
30
31                         GetILOffsetFunc get_il_offset_func;
32                         GetGuidFunc get_guid_func;
33
34                         ModuleBuilder module_builder;
35
36                         public SymbolWriterImpl (ModuleBuilder module_builder, string filename)
37                                 : base (filename)
38                         {
39                                 this.module_builder = module_builder;
40                         }
41
42                         public int GetILOffset (ILGenerator ig)
43                         {
44                                 return get_il_offset_func (ig);
45                         }
46
47                         public void WriteSymbolFile ()
48                         {
49                                 Guid guid = get_guid_func (module_builder);
50                                 WriteSymbolFile (guid);
51                         }
52
53                         public bool Initialize ()
54                         {
55                                 MethodInfo mi = typeof (ILGenerator).GetMethod (
56                                         "Mono_GetCurrentOffset",
57                                         BindingFlags.Static | BindingFlags.NonPublic);
58                                 if (mi == null)
59                                         return false;
60
61                                 get_il_offset_func = (GetILOffsetFunc) System.Delegate.CreateDelegate (
62                                         typeof (GetILOffsetFunc), mi);
63
64                                 mi = typeof (ModuleBuilder).GetMethod (
65                                         "Mono_GetGuid",
66                                         BindingFlags.Static | BindingFlags.NonPublic);
67                                 if (mi == null)
68                                         return false;
69
70                                 get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
71                                         typeof (GetGuidFunc), mi);
72
73                                 Location.DefineSymbolDocuments (this);
74
75                                 return true;
76                         }
77                 }
78
79                 public static void DefineLocalVariable (string name, LocalBuilder builder)
80                 {
81                         if (symwriter != null) {
82                                 int index = MonoDebuggerSupport.GetLocalIndex (builder);
83                                 symwriter.DefineLocalVariable (index, name);
84                         }
85                 }
86
87                 public static void OpenMethod (ISourceFile file, ISourceMethod method,
88                                                int start_row, int start_column,
89                                                int end_row, int end_column)
90                 {
91                         if (symwriter != null)
92                                 symwriter.OpenMethod (file, method, start_row, start_column,
93                                                       end_row, end_column);
94                 }
95
96                 public static void CloseMethod ()
97                 {
98                         if (symwriter != null)
99                                 symwriter.CloseMethod ();
100                 }
101
102                 public static int OpenScope (ILGenerator ig)
103                 {
104                         if (symwriter != null) {
105                                 int offset = symwriter.GetILOffset (ig);
106                                 return symwriter.OpenScope (offset);
107                         } else {
108                                 return -1;
109                         }
110                 }
111
112                 public static void CloseScope (ILGenerator ig)
113                 {
114                         if (symwriter != null) {
115                                 int offset = symwriter.GetILOffset (ig);
116                                 symwriter.CloseScope (offset);
117                         }
118                 }
119
120                 public static int DefineNamespace (string name, SourceFileEntry source,
121                                                    string[] using_clauses, int parent)
122                 {
123                         if (symwriter != null)
124                                 return symwriter.DefineNamespace (name, source, using_clauses, parent);
125                         else
126                                 return -1;
127                 }
128
129 #region Terrania additions
130                 public static void DefineAnonymousScope (int id)
131                 {
132                         if (symwriter != null)
133                                 symwriter.DefineAnonymousScope (id);
134                 }
135
136                 public static void DefineScopeVariable (int scope, LocalBuilder builder)
137                 {
138                         if (symwriter != null) {
139                                 int index = MonoDebuggerSupport.GetLocalIndex (builder);
140                                 symwriter.DefineScopeVariable (scope, index);
141                         }
142                 }
143
144                 public static void DefineScopeVariable (int scope)
145                 {
146                         if (symwriter != null)
147                                 symwriter.DefineScopeVariable (scope, -1);
148                 }
149
150                 public static void DefineCapturedLocal (int scope_id, string name,
151                                                         string captured_name)
152                 {
153                         if (symwriter != null)
154                                 symwriter.DefineCapturedLocal (scope_id, name, captured_name);
155                 }
156
157                 public static void DefineCapturedParameter (int scope_id, string name,
158                                                             string captured_name)
159                 {
160                         if (symwriter != null)
161                                 symwriter.DefineCapturedParameter (scope_id, name, captured_name);
162                 }
163
164                 public static void DefineCapturedThis (int scope_id, string captured_name)
165                 {
166                         if (symwriter != null)
167                                 symwriter.DefineCapturedThis (scope_id, captured_name);
168                 }
169
170                 public static void DefineCapturedScope (int scope_id, int id, string captured_name)
171                 {
172                         if (symwriter != null)
173                                 symwriter.DefineCapturedScope (scope_id, id, captured_name);
174                 }
175
176                 public static void SetRealMethodName (string name)
177                 {
178                         if (symwriter != null)
179                                 symwriter.SetRealMethodName (name);
180                 }
181
182                 public static void OpenCompilerGeneratedBlock (ILGenerator ig)
183                 {
184                         if (symwriter != null) {
185                                 int offset = symwriter.GetILOffset (ig);
186                                 symwriter.OpenCompilerGeneratedBlock (offset);
187                         }
188                 }
189
190                 public static void CloseCompilerGeneratedBlock (ILGenerator ig)
191                 {
192                         if (symwriter != null) {
193                                 int offset = symwriter.GetILOffset (ig);
194                                 symwriter.CloseCompilerGeneratedBlock (offset);
195                         }
196                 }
197
198                 public static void StartIteratorBody (ILGenerator ig)
199                 {
200                         if (symwriter != null) {
201                                 int offset = symwriter.GetILOffset (ig);
202                                 symwriter.StartIteratorBody (offset);
203                         }
204                 }
205
206                 public static void EndIteratorBody (ILGenerator ig)
207                 {
208                         if (symwriter != null) {
209                                 int offset = symwriter.GetILOffset (ig);
210                                 symwriter.EndIteratorBody (offset);
211                         }
212                 }
213
214                 public static void StartIteratorDispatcher (ILGenerator ig)
215                 {
216                         if (symwriter != null) {
217                                 int offset = symwriter.GetILOffset (ig);
218                                 symwriter.StartIteratorDispatcher (offset);
219                         }
220                 }
221
222                 public static void EndIteratorDispatcher (ILGenerator ig)
223                 {
224                         if (symwriter != null) {
225                                 int offset = symwriter.GetILOffset (ig);
226                                 symwriter.EndIteratorDispatcher (offset);
227                         }
228                 }
229 #endregion
230
231                 public static void MarkSequencePoint (ILGenerator ig, int row, int column)
232                 {
233                         if (symwriter != null) {
234                                 int offset = symwriter.GetILOffset (ig);
235                                 symwriter.MarkSequencePoint (offset, row, column);
236                         }
237                 }
238
239                 public static void WriteSymbolFile ()
240                 {
241                         if (symwriter != null)
242                                 symwriter.WriteSymbolFile ();
243                 }
244
245                 public static bool Initialize (ModuleBuilder module, string filename)
246                 {
247                         symwriter = new SymbolWriterImpl (module, filename);
248                         if (!symwriter.Initialize ()) {
249                                 symwriter = null;
250                                 return false;
251                         }
252
253                         return true;
254                 }
255         }
256 }