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