Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Compiler / SymbolDocumentGenerator.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Apache License, Version 2.0, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15 #if FEATURE_REFEMIT && FEATURE_PDBEMIT
16
17 #if !FEATURE_CORE_DLR
18 using Microsoft.Scripting.Ast;
19 using Microsoft.Scripting.Ast.Compiler;
20 #else
21 using System.Linq.Expressions;
22 using System.Linq.Expressions.Compiler;
23 #endif
24
25 using System.Collections.Generic;
26 using System.Diagnostics;
27 using System.Diagnostics.SymbolStore;
28 using System.Reflection;
29 using System.Reflection.Emit;
30
31 namespace System.Runtime.CompilerServices {
32 #if !FEATURE_CORE_DLR || SILVERLIGHT
33     using ILGenerator = OffsetTrackingILGenerator;
34 #endif
35
36     /// <summary>
37     /// Generator of PDB debugging information for expression trees.
38     /// </summary>
39     internal sealed class SymbolDocumentGenerator : DebugInfoGenerator {
40         private Dictionary<SymbolDocumentInfo, ISymbolDocumentWriter> _symbolWriters;
41
42         private ISymbolDocumentWriter GetSymbolWriter(MethodBuilder method, SymbolDocumentInfo document) {
43             ISymbolDocumentWriter result;
44             if (_symbolWriters == null) {
45                 _symbolWriters = new Dictionary<SymbolDocumentInfo, ISymbolDocumentWriter>();
46             }
47
48             if (!_symbolWriters.TryGetValue(document, out result)) {
49                 result = ((ModuleBuilder)method.Module).DefineDocument(document.FileName, document.Language, document.LanguageVendor, SymbolGuids.DocumentType_Text);
50                 _symbolWriters.Add(document, result);
51             }
52
53             return result;
54         }
55
56         internal override void MarkSequencePoint(LambdaExpression method, MethodBase methodBase, ILGenerator ilg, DebugInfoExpression sequencePoint) {
57             MethodBuilder builder = methodBase as MethodBuilder;
58             if (builder != null) {
59                 ilg.MarkSequencePoint(GetSymbolWriter(builder, sequencePoint.Document), sequencePoint.StartLine, sequencePoint.StartColumn, sequencePoint.EndLine, sequencePoint.EndColumn);
60             }
61         }
62
63         public override void MarkSequencePoint(LambdaExpression method, int ilOffset, DebugInfoExpression sequencePoint) {
64             throw Error.PdbGeneratorNeedsExpressionCompiler();
65         }
66
67         internal override void SetLocalName(LocalBuilder localBuilder, string name) {
68             localBuilder.SetLocalSymInfo(name);
69         }
70     }
71 }
72 #endif