1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using System.Diagnostics;
18 using System.Dynamic.Utils;
21 namespace Microsoft.Scripting.Ast {
23 namespace System.Linq.Expressions {
26 /// Emits or clears a sequence point for debug information.
28 /// This allows the debugger to highlight the correct source code when
31 [DebuggerTypeProxy(typeof(Expression.DebugInfoExpressionProxy))]
32 public class DebugInfoExpression : Expression {
34 private readonly SymbolDocumentInfo _document;
36 internal DebugInfoExpression(SymbolDocumentInfo document) {
41 /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
43 /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
44 public sealed override Type Type {
45 get { return typeof(void); }
49 /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
51 /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
52 public sealed override ExpressionType NodeType {
53 get { return ExpressionType.DebugInfo; }
57 /// Gets the start line of this <see cref="DebugInfoExpression" />.
59 public virtual int StartLine {
60 get { throw ContractUtils.Unreachable; }
64 /// Gets the start column of this <see cref="DebugInfoExpression" />.
66 public virtual int StartColumn {
67 get { throw ContractUtils.Unreachable; }
71 /// Gets the end line of this <see cref="DebugInfoExpression" />.
73 public virtual int EndLine {
74 get { throw ContractUtils.Unreachable; }
78 /// Gets the end column of this <see cref="DebugInfoExpression" />.
80 public virtual int EndColumn {
81 get { throw ContractUtils.Unreachable; }
85 /// Gets the <see cref="SymbolDocumentInfo"/> that represents the source file.
87 public SymbolDocumentInfo Document {
88 get { return _document; }
92 /// Gets the value to indicate if the <see cref="DebugInfoExpression"/> is for clearing a sequence point.
94 public virtual bool IsClear {
95 get { throw ContractUtils.Unreachable; }
99 /// Dispatches to the specific visit method for this node type.
101 protected internal override Expression Accept(ExpressionVisitor visitor) {
102 return visitor.VisitDebugInfo(this);
106 #region Specialized subclasses
108 internal sealed class SpanDebugInfoExpression : DebugInfoExpression {
109 private readonly int _startLine, _startColumn, _endLine, _endColumn;
111 internal SpanDebugInfoExpression(SymbolDocumentInfo document, int startLine, int startColumn, int endLine, int endColumn)
113 _startLine = startLine;
114 _startColumn = startColumn;
116 _endColumn = endColumn;
119 public override int StartLine {
125 public override int StartColumn {
131 public override int EndLine {
137 public override int EndColumn {
143 public override bool IsClear {
149 protected internal override Expression Accept(ExpressionVisitor visitor) {
150 return visitor.VisitDebugInfo(this);
154 internal sealed class ClearDebugInfoExpression : DebugInfoExpression {
155 internal ClearDebugInfoExpression(SymbolDocumentInfo document)
159 public override bool IsClear {
165 public override int StartLine {
171 public override int StartColumn {
177 public override int EndLine {
183 public override int EndColumn {
191 public partial class Expression {
193 /// Creates a <see cref="DebugInfoExpression"/> with the specified span.
195 /// <param name="document">The <see cref="SymbolDocumentInfo"/> that represents the source file.</param>
196 /// <param name="startLine">The start line of this <see cref="DebugInfoExpression" />. Must be greater than 0.</param>
197 /// <param name="startColumn">The start column of this <see cref="DebugInfoExpression" />. Must be greater than 0.</param>
198 /// <param name="endLine">The end line of this <see cref="DebugInfoExpression" />. Must be greater or equal than the start line.</param>
199 /// <param name="endColumn">The end column of this <see cref="DebugInfoExpression" />. If the end line is the same as the start line, it must be greater or equal than the start column. In any case, must be greater than 0.</param>
200 /// <returns>An instance of <see cref="DebugInfoExpression"/>.</returns>
201 public static DebugInfoExpression DebugInfo(SymbolDocumentInfo document, int startLine, int startColumn, int endLine, int endColumn) {
202 ContractUtils.RequiresNotNull(document, "document");
203 if (startLine == 0xfeefee && startColumn == 0 && endLine == 0xfeefee && endColumn == 0) {
204 return new ClearDebugInfoExpression(document);
207 ValidateSpan(startLine, startColumn, endLine, endColumn);
208 return new SpanDebugInfoExpression(document, startLine, startColumn, endLine, endColumn);
212 /// Creates a <see cref="DebugInfoExpression"/> for clearing a sequence point.
214 /// <param name="document">The <see cref="SymbolDocumentInfo"/> that represents the source file.</param>
215 /// <returns>An instance of <see cref="DebugInfoExpression"/> for clearning a sequence point.</returns>
216 public static DebugInfoExpression ClearDebugInfo(SymbolDocumentInfo document) {
217 ContractUtils.RequiresNotNull(document, "document");
219 return new ClearDebugInfoExpression(document);
222 private static void ValidateSpan(int startLine, int startColumn, int endLine, int endColumn) {
224 throw Error.OutOfRange("startLine", 1);
226 if (startColumn < 1) {
227 throw Error.OutOfRange("startColumn", 1);
230 throw Error.OutOfRange("endLine", 1);
233 throw Error.OutOfRange("endColumn", 1);
235 if (startLine > endLine) {
236 throw Error.StartEndMustBeOrdered();
238 if (startLine == endLine && startColumn > endColumn) {
239 throw Error.StartEndMustBeOrdered();