Don't emit reaonly. prefix for reference loads
[mono.git] / mcs / mcs / symbolwriter.cs
1 //
2 // symbolwriter.cs: The debug symbol writer
3 //
4 // Authors: Martin Baulig (martin@ximian.com)
5 //          Marek Safar (marek.safar@gmail.com)
6 //
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2004-2010 Novell, Inc
11 //
12
13 using System;
14
15 #if STATIC
16 using IKVM.Reflection;
17 using IKVM.Reflection.Emit;
18 #else
19 using System.Reflection;
20 using System.Reflection.Emit;
21 #endif
22
23 using Mono.CompilerServices.SymbolWriter;
24
25 namespace Mono.CSharp
26 {
27         static class SymbolWriter
28         {
29 #if !NET_4_0 && !STATIC
30                 delegate int GetILOffsetFunc (ILGenerator ig);
31                 static GetILOffsetFunc get_il_offset_func;
32
33                 delegate Guid GetGuidFunc (ModuleBuilder mb);
34                 static GetGuidFunc get_guid_func;
35
36                 static void Initialize ()
37                 {
38                         var mi = typeof (ILGenerator).GetMethod (
39                                 "Mono_GetCurrentOffset",
40                                 BindingFlags.Static | BindingFlags.NonPublic);
41                         if (mi == null)
42                                 throw new MissingMethodException ("Mono_GetCurrentOffset");
43
44                         get_il_offset_func = (GetILOffsetFunc) System.Delegate.CreateDelegate (
45                                 typeof (GetILOffsetFunc), mi);
46
47                         mi = typeof (ModuleBuilder).GetMethod (
48                                 "Mono_GetGuid",
49                                 BindingFlags.Static | BindingFlags.NonPublic);
50                         if (mi == null)
51                                 throw new MissingMethodException ("Mono_GetGuid");
52
53                         get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
54                                 typeof (GetGuidFunc), mi);
55                 }
56 #endif
57
58                 static int GetILOffset (ILGenerator ig)
59                 {
60 #if NET_4_0 || STATIC
61                         return ig.ILOffset;
62 #else
63                         if (get_il_offset_func == null)
64                                 Initialize ();
65
66                         return get_il_offset_func (ig);
67 #endif
68                 }
69
70                 public static Guid GetGuid (ModuleBuilder module)
71                 {
72 #if NET_4_0 || STATIC
73                         return module.ModuleVersionId;
74 #else
75                         if (get_guid_func == null)
76                                 Initialize ();
77
78                         return get_guid_func (module);
79 #endif
80                 }
81
82                 public static bool HasSymbolWriter {
83                         get { return symwriter != null; }
84                 }
85
86                 public static MonoSymbolWriter symwriter;
87
88                 public static void DefineLocalVariable (string name, LocalBuilder builder)
89                 {
90                         if (symwriter != null) {
91                                 symwriter.DefineLocalVariable (builder.LocalIndex, name);
92                         }
93                 }
94
95                 public static SourceMethodBuilder OpenMethod (ICompileUnit file, int ns_id,
96                                                               IMethodDef method)
97                 {
98                         if (symwriter != null)
99                                 return symwriter.OpenMethod (file, ns_id, method);
100                         else
101                                 return null;
102                 }
103
104                 public static void CloseMethod ()
105                 {
106                         if (symwriter != null)
107                                 symwriter.CloseMethod ();
108                 }
109
110                 public static int OpenScope (ILGenerator ig)
111                 {
112                         if (symwriter != null) {
113                                 int offset = GetILOffset (ig);
114                                 return symwriter.OpenScope (offset);
115                         } else {
116                                 return -1;
117                         }
118                 }
119
120                 public static void CloseScope (ILGenerator ig)
121                 {
122                         if (symwriter != null) {
123                                 int offset = GetILOffset (ig);
124                                 symwriter.CloseScope (offset);
125                         }
126                 }
127
128                 public static int DefineNamespace (string name, CompileUnitEntry source,
129                                                    string[] using_clauses, int parent)
130                 {
131                         if (symwriter != null)
132                                 return symwriter.DefineNamespace (name, source, using_clauses, parent);
133                         else
134                                 return -1;
135                 }
136
137                 public static void DefineAnonymousScope (int id)
138                 {
139                         if (symwriter != null)
140                                 symwriter.DefineAnonymousScope (id);
141                 }
142
143                 public static void DefineScopeVariable (int scope, LocalBuilder builder)
144                 {
145                         if (symwriter != null) {
146                                 symwriter.DefineScopeVariable (scope, builder.LocalIndex);
147                         }
148                 }
149
150                 public static void DefineScopeVariable (int scope)
151                 {
152                         if (symwriter != null)
153                                 symwriter.DefineScopeVariable (scope, -1);
154                 }
155
156                 public static void DefineCapturedLocal (int scope_id, string name,
157                                                         string captured_name)
158                 {
159                         if (symwriter != null)
160                                 symwriter.DefineCapturedLocal (scope_id, name, captured_name);
161                 }
162
163                 public static void DefineCapturedParameter (int scope_id, string name,
164                                                             string captured_name)
165                 {
166                         if (symwriter != null)
167                                 symwriter.DefineCapturedParameter (scope_id, name, captured_name);
168                 }
169
170                 public static void DefineCapturedThis (int scope_id, string captured_name)
171                 {
172                         if (symwriter != null)
173                                 symwriter.DefineCapturedThis (scope_id, captured_name);
174                 }
175
176                 public static void DefineCapturedScope (int scope_id, int id, string captured_name)
177                 {
178                         if (symwriter != null)
179                                 symwriter.DefineCapturedScope (scope_id, id, captured_name);
180                 }
181
182                 public static void OpenCompilerGeneratedBlock (EmitContext ec)
183                 {
184                         if (symwriter != null) {
185                                 int offset = GetILOffset (ec.ig);
186                                 symwriter.OpenCompilerGeneratedBlock (offset);
187                         }
188                 }
189
190                 public static void CloseCompilerGeneratedBlock (EmitContext ec)
191                 {
192                         if (symwriter != null) {
193                                 int offset = GetILOffset (ec.ig);
194                                 symwriter.CloseCompilerGeneratedBlock (offset);
195                         }
196                 }
197
198                 public static void StartIteratorBody (EmitContext ec)
199                 {
200                         if (symwriter != null) {
201                                 int offset = GetILOffset (ec.ig);
202                                 symwriter.StartIteratorBody (offset);
203                         }
204                 }
205
206                 public static void EndIteratorBody (EmitContext ec)
207                 {
208                         if (symwriter != null) {
209                                 int offset = GetILOffset (ec.ig);
210                                 symwriter.EndIteratorBody (offset);
211                         }
212                 }
213
214                 public static void StartIteratorDispatcher (EmitContext ec)
215                 {
216                         if (symwriter != null) {
217                                 int offset = GetILOffset (ec.ig);
218                                 symwriter.StartIteratorDispatcher (offset);
219                         }
220                 }
221
222                 public static void EndIteratorDispatcher (EmitContext ec)
223                 {
224                         if (symwriter != null) {
225                                 int offset = GetILOffset (ec.ig);
226                                 symwriter.EndIteratorDispatcher (offset);
227                         }
228                 }
229
230                 public static void MarkSequencePoint (ILGenerator ig, Location loc)
231                 {
232                         if (symwriter != null) {
233                                 SourceFileEntry file = loc.SourceFile.SourceFileEntry;
234                                 int offset = GetILOffset (ig);
235                                 symwriter.MarkSequencePoint (
236                                         offset, file, loc.Row, loc.Column, loc.Hidden);
237                         }
238                 }
239
240                 public static void Reset ()
241                 {
242                         symwriter = null;
243                 }
244         }
245 }