2 Copyright (C) 2009 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
26 using System.Collections.Generic;
27 using Mono.CompilerServices.SymbolWriter;
28 using IKVM.Reflection.Emit;
30 namespace IKVM.Reflection.Impl
32 sealed class Method : IMethodDef
36 internal SymbolDocumentWriter document;
37 internal int[] offsets;
39 internal int[] columns;
40 internal List<string> variables = new List<string>();
53 sealed class SymbolDocumentWriter : System.Diagnostics.SymbolStore.ISymbolDocumentWriter
55 internal readonly string url;
56 internal SourceFileEntry source;
58 internal SymbolDocumentWriter(string url)
63 public void SetCheckSum(Guid algorithmId, byte[] checkSum)
67 public void SetSource(byte[] source)
72 sealed class MdbWriter : ISymbolWriterImpl
74 private readonly ModuleBuilder moduleBuilder;
75 private readonly Dictionary<int, Method> methods = new Dictionary<int, Method>();
76 private readonly Dictionary<string, SymbolDocumentWriter> documents = new Dictionary<string, SymbolDocumentWriter>();
77 private Method currentMethod;
79 internal MdbWriter(ModuleBuilder moduleBuilder)
81 this.moduleBuilder = moduleBuilder;
84 public byte[] GetDebugInfo(ref IMAGE_DEBUG_DIRECTORY idd)
86 return Empty<byte>.Array;
89 public void RemapToken(int oldToken, int newToken)
91 if (methods.ContainsKey(oldToken))
93 methods[oldToken].token = newToken;
99 MonoSymbolWriter writer = new MonoSymbolWriter(moduleBuilder.FullyQualifiedName);
101 foreach (Method method in methods.Values)
103 if (method.document != null)
105 if (method.document.source == null)
107 method.document.source = new SourceFileEntry(writer.SymbolFile, method.document.url);
109 ICompileUnit file = new CompileUnitEntry(writer.SymbolFile, method.document.source);
110 SourceMethodBuilder smb = writer.OpenMethod(file, 0, method);
111 for (int i = 0; i < method.offsets.Length; i++)
113 smb.MarkSequencePoint(method.offsets[i], method.document.source, method.lines[i], method.columns[i], false);
115 for (int i = 0; i < method.variables.Count; i++)
117 writer.DefineLocalVariable(i, method.variables[i]);
119 writer.CloseMethod();
123 writer.WriteSymbolFile(moduleBuilder.ModuleVersionId);
126 public System.Diagnostics.SymbolStore.ISymbolDocumentWriter DefineDocument(string url, Guid language, Guid languageVendor, Guid documentType)
128 SymbolDocumentWriter writer;
129 if (!documents.TryGetValue(url, out writer))
131 writer = new SymbolDocumentWriter(url);
132 documents.Add(url, writer);
137 public void OpenMethod(System.Diagnostics.SymbolStore.SymbolToken method)
139 throw new NotImplementedException();
142 public void OpenMethod(System.Diagnostics.SymbolStore.SymbolToken token, MethodBase mb)
144 Method method = new Method();
145 method.token = token.GetToken();
146 method.name = mb.Name;
147 methods.Add(token.GetToken(), method);
148 currentMethod = method;
151 public void CloseMethod()
153 currentMethod = null;
156 public void DefineLocalVariable(string name, System.Reflection.FieldAttributes attributes, byte[] signature, System.Diagnostics.SymbolStore.SymAddressKind addrKind, int addr1, int addr2, int addr3, int startOffset, int endOffset)
160 public void DefineLocalVariable2(string name, FieldAttributes attributes, int signature, System.Diagnostics.SymbolStore.SymAddressKind addrKind, int addr1, int addr2, int addr3, int startOffset, int endOffset)
162 currentMethod.variables.Add(name);
165 public void DefineSequencePoints(System.Diagnostics.SymbolStore.ISymbolDocumentWriter document, int[] offsets, int[] lines, int[] columns, int[] endLines, int[] endColumns)
167 currentMethod.document = (SymbolDocumentWriter)document;
168 currentMethod.offsets = offsets;
169 currentMethod.lines = lines;
170 currentMethod.columns = columns;
173 public void DefineParameter(string name, System.Reflection.ParameterAttributes attributes, int sequence, System.Diagnostics.SymbolStore.SymAddressKind addrKind, int addr1, int addr2, int addr3)
177 public void DefineField(System.Diagnostics.SymbolStore.SymbolToken parent, string name, System.Reflection.FieldAttributes attributes, byte[] signature, System.Diagnostics.SymbolStore.SymAddressKind addrKind, int addr1, int addr2, int addr3)
181 public void DefineGlobalVariable(string name, System.Reflection.FieldAttributes attributes, byte[] signature, System.Diagnostics.SymbolStore.SymAddressKind addrKind, int addr1, int addr2, int addr3)
185 public void OpenNamespace(string name)
189 public void CloseNamespace()
193 public void UsingNamespace(string fullName)
197 public int OpenScope(int startOffset)
202 public void CloseScope(int endOffset)
206 public void SetMethodSourceRange(System.Diagnostics.SymbolStore.ISymbolDocumentWriter startDoc, int startLine, int startColumn, System.Diagnostics.SymbolStore.ISymbolDocumentWriter endDoc, int endLine, int endColumn)
210 public void SetScopeRange(int scopeID, int startOffset, int endOffset)
214 public void SetSymAttribute(System.Diagnostics.SymbolStore.SymbolToken parent, string name, byte[] data)
218 public void SetUserEntryPoint(System.Diagnostics.SymbolStore.SymbolToken entryMethod)
222 public void SetUnderlyingWriter(IntPtr underlyingWriter)
224 throw new InvalidOperationException();
227 public void Initialize(IntPtr emitter, string filename, bool fFullBuild)
229 throw new InvalidOperationException();