5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc. http://www.novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
33 using System.Reflection.Emit;
34 using System.Runtime.CompilerServices;
35 using System.Collections;
37 using System.Diagnostics.SymbolStore;
39 namespace Mono.CompilerServices.SymbolWriter
41 public class SymbolWriterImpl: ISymbolWriter
46 delegate Guid GetGuidFunc (ModuleBuilder mb);
47 GetGuidFunc get_guid_func;
51 Stack namespaceStack = new Stack ();
54 Hashtable documents = new Hashtable ();
56 public SymbolWriterImpl (ModuleBuilder mb)
63 MethodInfo mi = typeof (ModuleBuilder).GetMethod (
65 BindingFlags.Static | BindingFlags.NonPublic);
69 get_guid_func = (GetGuidFunc) System.Delegate.CreateDelegate (
70 typeof (GetGuidFunc), mi);
72 msw.WriteSymbolFile (get_guid_func (mb));
75 public void CloseMethod ()
83 public void CloseNamespace ()
85 namespaceStack.Pop ();
86 msw.CloseNamespace ();
89 public void CloseScope (int endOffset)
91 msw.CloseScope (endOffset);
94 public ISymbolDocumentWriter DefineDocument (
100 SymbolDocumentWriterImpl doc = (SymbolDocumentWriterImpl) documents [url];
102 doc = new SymbolDocumentWriterImpl (msw.DefineDocument (url));
103 documents [url] = doc;
108 public void DefineField (
111 FieldAttributes attributes,
113 SymAddressKind addrKind,
120 public void DefineGlobalVariable (
122 FieldAttributes attributes,
124 SymAddressKind addrKind,
131 public void DefineLocalVariable (
133 FieldAttributes attributes,
135 SymAddressKind addrKind,
142 msw.DefineLocalVariable (name, signature);
145 public void DefineParameter (
147 ParameterAttributes attributes,
149 SymAddressKind addrKind,
156 public void DefineSequencePoints (
157 ISymbolDocumentWriter document,
164 for (int n=0; n<offsets.Length; n++) {
165 if (n > 0 && offsets[n] == offsets[n-1] && lines[n] == lines[n-1] && columns[n] == columns[n-1])
167 msw.MarkSequencePoint (offsets[n], lines[n], columns[n]);
171 public void Initialize (IntPtr emitter, string filename, bool fFullBuild)
173 msw = new MonoSymbolWriter (filename);
176 public void OpenMethod (SymbolToken method)
178 currentToken = method.GetToken ();
181 public void OpenNamespace (string name)
183 NamespaceInfo n = new NamespaceInfo ();
186 namespaceStack.Push (n);
189 public int OpenScope (int startOffset)
191 return msw.OpenScope (startOffset);
194 public void SetMethodSourceRange (
195 ISymbolDocumentWriter startDoc,
198 ISymbolDocumentWriter endDoc,
202 SourceMethodImpl sm = new SourceMethodImpl (methodName, currentToken, GetCurrentNamespace (startDoc));
203 msw.OpenMethod (startDoc as ISourceFile, sm, startLine, startColumn, endLine, endColumn);
207 public void SetScopeRange (int scopeID, int startOffset, int endOffset)
211 public void SetSymAttribute (SymbolToken parent, string name, byte[] data)
213 // This is a hack! but MonoSymbolWriter needs the method name
214 // and ISymbolWriter does not have any method for providing it
215 if (name == "__name")
216 methodName = System.Text.Encoding.UTF8.GetString (data);
219 public void SetUnderlyingWriter (IntPtr underlyingWriter)
223 public void SetUserEntryPoint (SymbolToken entryMethod)
227 public void UsingNamespace (string fullName)
229 if (namespaceStack.Count == 0) {
233 NamespaceInfo ni = (NamespaceInfo) namespaceStack.Peek ();
234 if (ni.NamespaceID != -1) {
235 NamespaceInfo old = ni;
237 OpenNamespace (old.Name);
238 ni = (NamespaceInfo) namespaceStack.Peek ();
239 ni.UsingClauses = old.UsingClauses;
241 ni.UsingClauses.Add (fullName);
244 int GetCurrentNamespace (ISymbolDocumentWriter doc)
246 if (namespaceStack.Count == 0) {
250 NamespaceInfo ni = (NamespaceInfo) namespaceStack.Peek ();
251 if (ni.NamespaceID == -1)
253 string[] usings = (string[]) ni.UsingClauses.ToArray (typeof(string));
256 if (namespaceStack.Count > 1) {
257 namespaceStack.Pop ();
258 parentId = ((NamespaceInfo) namespaceStack.Peek ()).NamespaceID;
259 namespaceStack.Push (ni);
262 ni.NamespaceID = msw.DefineNamespace (ni.Name, ((ISourceFile)doc).Entry, usings, parentId);
264 return ni.NamespaceID;
268 class SymbolDocumentWriterImpl: ISymbolDocumentWriter, ISourceFile
270 SourceFileEntry entry;
272 public SymbolDocumentWriterImpl (SourceFileEntry e)
277 public void SetCheckSum (Guid algorithmId, byte[] checkSum)
281 public void SetSource (byte[] source)
285 public SourceFileEntry Entry {
286 get { return entry; }
290 class SourceMethodImpl: ISourceMethod
296 public SourceMethodImpl (string name, int token, int namespaceID)
300 this.namespaceID = namespaceID;
307 public int NamespaceID {
308 get { return namespaceID; }
312 get { return token; }
319 public int NamespaceID;
320 public ArrayList UsingClauses = new ArrayList ();