2 // location.cs: Keeps track of the location of source code entity
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // (C) 2001 Ximian, Inc.
9 // (C) 2005 Novell, Inc.
14 using System.Collections;
15 using Mono.CompilerServices.SymbolWriter;
17 namespace Mono.CSharp {
19 /// This is one single source file.
22 /// This is intentionally a class and not a struct since we need
23 /// to pass this by reference.
25 public sealed class SourceFile : ISourceFile {
26 public readonly string Name;
27 public readonly string Path;
28 public readonly int Index;
29 public SourceFileEntry SourceFileEntry;
31 public SourceFile (string name, string path, int index)
38 SourceFileEntry ISourceFile.Entry {
39 get { return SourceFileEntry; }
42 public override string ToString ()
44 return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
45 Name, Path, Index, SourceFileEntry);
50 /// Keeps track of the location in the program
54 /// This uses a compact representation and a couple of auxiliary
55 /// structures to keep track of tokens to (file,line and column)
56 /// mappings. The usage of the bits is:
58 /// - 16 bits for "checkpoint" which is a mixed concept of
59 /// file and "line segment"
60 /// - 8 bits for line delta (offset) from the line segment
61 /// - 8 bits for column number.
63 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
65 public struct Location {
69 public readonly int LineOffset;
70 public readonly int File;
72 public Checkpoint (int file, int line)
75 LineOffset = line - (int) (line % (1 << line_delta_bits));
79 static ArrayList source_list;
80 static Hashtable source_files;
81 static int checkpoint_bits;
82 static int source_count;
83 static int current_source;
84 static int line_delta_bits;
85 static int line_delta_mask;
86 static int column_bits;
87 static int column_mask;
88 static Checkpoint [] checkpoints;
89 static int checkpoint_index;
91 public readonly static Location Null = new Location (-1);
92 public static bool InEmacs;
96 source_files = new Hashtable ();
97 source_list = new ArrayList ();
99 checkpoints = new Checkpoint [10];
102 public static void Reset ()
104 source_files = new Hashtable ();
105 source_list = new ArrayList ();
111 // This must be called before parsing/tokenizing any files.
113 static public void AddFile (string name)
115 string path = Path.GetFullPath (name);
117 if (source_files.Contains (path)){
118 int id = (int) source_files [path];
119 string other_name = ((SourceFile) source_list [id - 1]).Name;
120 if (name.Equals (other_name))
121 Report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
123 Report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
127 source_files.Add (path, ++source_count);
128 source_list.Add (new SourceFile (name, path, source_count));
131 static public SourceFile[] SourceFiles {
133 SourceFile[] retval = new SourceFile [source_list.Count];
134 source_list.CopyTo (retval, 0);
140 // After adding all source files we want to compile with AddFile(), this method
141 // must be called to `reserve' an appropriate number of bits in the token for the
142 // source file. We reserve some extra space for files we encounter via #line
143 // directives while parsing.
145 static public void Initialize ()
147 checkpoints = new Checkpoint [source_list.Count * 2];
148 if (checkpoints.Length > 0)
149 checkpoints [0] = new Checkpoint (0, 0);
154 line_delta_mask = 0xFF00;
155 checkpoint_index = 0;
156 checkpoint_bits = 16;
160 // This is used when we encounter a #line preprocessing directive.
162 static public SourceFile LookupFile (string name)
164 string path = name.Length == 0 ? "" : Path.GetFullPath (name);
166 if (!source_files.Contains (path)) {
167 if (source_count >= (1 << checkpoint_bits))
168 return new SourceFile (name, path, 0);
170 source_files.Add (path, ++source_count);
171 SourceFile retval = new SourceFile (name, path, source_count);
172 source_list.Add (retval);
176 int index = (int) source_files [path];
177 return (SourceFile) source_list [index - 1];
180 static public void Push (SourceFile file)
182 current_source = file.Index;
183 // File is always pushed before being changed.
187 // If we're compiling with debugging support, this is called between parsing
188 // and code generation to register all the source files with the
191 static public void DefineSymbolDocuments (MonoSymbolWriter symwriter)
193 foreach (SourceFile file in source_list) {
194 file.SourceFileEntry = symwriter.DefineDocument (file.Path);
198 public Location (int row)
203 public Location (int row, int column)
212 int max = checkpoint_index < 10 ?
213 checkpoint_index : 10;
214 for (int i = 0; i < max; i++) {
215 int offset = checkpoints [checkpoint_index - i].LineOffset;
216 delta = row - offset;
218 delta < (1 << line_delta_bits) &&
219 checkpoints [checkpoint_index - i].File == current_source) {
220 target = checkpoint_index - i;
225 AddCheckpoint (current_source, row);
226 target = checkpoint_index;
227 delta = row % (1 << line_delta_bits);
230 (long) (delta << column_bits) +
231 (long) (target << (line_delta_bits + column_bits));
232 token = l > 0xFFFFFFFF ? 0 : (int) l;
236 static void AddCheckpoint (int file, int row)
238 if (checkpoints.Length == ++checkpoint_index) {
239 Checkpoint [] tmp = new Checkpoint [checkpoint_index * 2];
240 Array.Copy (checkpoints, tmp, checkpoints.Length);
243 checkpoints [checkpoint_index] = new Checkpoint (file, row);
246 public override string ToString ()
248 if (column_bits == 0 || InEmacs)
249 return Name + "(" + Row.ToString () + "):";
251 return Name + "(" + Row.ToString () + "," + Column.ToString () +
252 (Column == column_mask ? "+):" : "):");
256 /// Whether the Location is Null
259 get { return token == 0; }
265 if (token == 0 || index == 0)
268 SourceFile file = (SourceFile) source_list [index - 1];
273 int CheckpointIndex {
274 get { return (int) ((token & 0xFFFF0000) >> (line_delta_bits + column_bits)); }
281 return checkpoints [CheckpointIndex].LineOffset + ((token & line_delta_mask) >> column_bits);
289 return (int) (token & column_mask);
297 if (checkpoints.Length <= CheckpointIndex) throw new Exception (String.Format ("Should not happen. Token is {0:X04}, checkpoints are {1}, index is {2}", token, checkpoints.Length, CheckpointIndex));
298 return checkpoints [CheckpointIndex].File;
302 // The ISymbolDocumentWriter interface is used by the symbol writer to
303 // describe a single source file - for each source file there's exactly
304 // one corresponding ISymbolDocumentWriter instance.
306 // This class has an internal hash table mapping source document names
307 // to such ISymbolDocumentWriter instances - so there's exactly one
308 // instance per document.
310 // This property returns the ISymbolDocumentWriter instance which belongs
311 // to the location's source file.
313 // If we don't have a symbol writer, this property is always null.
314 public SourceFile SourceFile {
319 return (SourceFile) source_list [index - 1];
324 public class LocatedToken
326 public readonly Location Location;
327 public readonly string Value;
329 public LocatedToken (Location loc, string value)
335 public override string ToString ()
337 return Location.ToString () + Value;