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;
25 namespace Microsoft.Scripting.Ast {
27 namespace System.Linq.Expressions {
30 /// Emits or clears a sequence point for debug information.
32 /// This allows the debugger to highlight the correct source code when
36 [DebuggerTypeProxy(typeof(Expression.DebugInfoExpressionProxy))]
38 public class DebugInfoExpression : Expression {
40 private readonly SymbolDocumentInfo _document;
42 internal DebugInfoExpression(SymbolDocumentInfo document) {
47 /// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
49 /// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
50 public sealed override Type Type {
51 get { return typeof(void); }
55 /// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
57 /// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
58 public sealed override ExpressionType NodeType {
59 get { return ExpressionType.DebugInfo; }
63 /// Gets the start line of this <see cref="DebugInfoExpression" />.
65 public virtual int StartLine {
66 get { throw ContractUtils.Unreachable; }
70 /// Gets the start column of this <see cref="DebugInfoExpression" />.
72 public virtual int StartColumn {
73 get { throw ContractUtils.Unreachable; }
77 /// Gets the end line of this <see cref="DebugInfoExpression" />.
79 public virtual int EndLine {
80 get { throw ContractUtils.Unreachable; }
84 /// Gets the end column of this <see cref="DebugInfoExpression" />.
86 public virtual int EndColumn {
87 get { throw ContractUtils.Unreachable; }
91 /// Gets the <see cref="SymbolDocumentInfo"/> that represents the source file.
93 public SymbolDocumentInfo Document {
94 get { return _document; }
98 /// Gets the value to indicate if the <see cref="DebugInfoExpression"/> is for clearing a sequence point.
100 public virtual bool IsClear {
101 get { throw ContractUtils.Unreachable; }
105 /// Dispatches to the specific visit method for this node type.
107 protected internal override Expression Accept(ExpressionVisitor visitor) {
108 return visitor.VisitDebugInfo(this);
112 #region Specialized subclasses
114 internal sealed class SpanDebugInfoExpression : DebugInfoExpression {
115 private readonly int _startLine, _startColumn, _endLine, _endColumn;
117 internal SpanDebugInfoExpression(SymbolDocumentInfo document, int startLine, int startColumn, int endLine, int endColumn)
119 _startLine = startLine;
120 _startColumn = startColumn;
122 _endColumn = endColumn;
125 public override int StartLine {
131 public override int StartColumn {
137 public override int EndLine {
143 public override int EndColumn {
149 public override bool IsClear {
155 protected internal override Expression Accept(ExpressionVisitor visitor) {
156 return visitor.VisitDebugInfo(this);
160 internal sealed class ClearDebugInfoExpression : DebugInfoExpression {
161 internal ClearDebugInfoExpression(SymbolDocumentInfo document)
165 public override bool IsClear {
171 public override int StartLine {
177 public override int StartColumn {
183 public override int EndLine {
189 public override int EndColumn {
197 public partial class Expression {
199 /// Creates a <see cref="DebugInfoExpression"/> with the specified span.
201 /// <param name="document">The <see cref="SymbolDocumentInfo"/> that represents the source file.</param>
202 /// <param name="startLine">The start line of this <see cref="DebugInfoExpression" />. Must be greater than 0.</param>
203 /// <param name="startColumn">The start column of this <see cref="DebugInfoExpression" />. Must be greater than 0.</param>
204 /// <param name="endLine">The end line of this <see cref="DebugInfoExpression" />. Must be greater or equal than the start line.</param>
205 /// <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>
206 /// <returns>An instance of <see cref="DebugInfoExpression"/>.</returns>
207 public static DebugInfoExpression DebugInfo(SymbolDocumentInfo document, int startLine, int startColumn, int endLine, int endColumn) {
208 ContractUtils.RequiresNotNull(document, "document");
209 if (startLine == 0xfeefee && startColumn == 0 && endLine == 0xfeefee && endColumn == 0) {
210 return new ClearDebugInfoExpression(document);
213 ValidateSpan(startLine, startColumn, endLine, endColumn);
214 return new SpanDebugInfoExpression(document, startLine, startColumn, endLine, endColumn);
218 /// Creates a <see cref="DebugInfoExpression"/> for clearing a sequence point.
220 /// <param name="document">The <see cref="SymbolDocumentInfo"/> that represents the source file.</param>
221 /// <returns>An instance of <see cref="DebugInfoExpression"/> for clearning a sequence point.</returns>
222 public static DebugInfoExpression ClearDebugInfo(SymbolDocumentInfo document) {
223 ContractUtils.RequiresNotNull(document, "document");
225 return new ClearDebugInfoExpression(document);
228 private static void ValidateSpan(int startLine, int startColumn, int endLine, int endColumn) {
230 throw Error.OutOfRange("startLine", 1);
232 if (startColumn < 1) {
233 throw Error.OutOfRange("startColumn", 1);
236 throw Error.OutOfRange("endLine", 1);
239 throw Error.OutOfRange("endColumn", 1);
241 if (startLine > endLine) {
242 throw Error.StartEndMustBeOrdered();
244 if (startLine == endLine && startColumn > endColumn) {
245 throw Error.StartEndMustBeOrdered();