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;
30 public bool HasLineDirective;
32 public SourceFile (string name, string path, int index)
39 SourceFileEntry ISourceFile.Entry {
40 get { return SourceFileEntry; }
43 public override string ToString ()
45 return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
46 Name, Path, Index, SourceFileEntry);
51 /// Keeps track of the location in the program
55 /// This uses a compact representation and a couple of auxiliary
56 /// structures to keep track of tokens to (file,line and column)
57 /// mappings. The usage of the bits is:
59 /// - 16 bits for "checkpoint" which is a mixed concept of
60 /// file and "line segment"
61 /// - 8 bits for line delta (offset) from the line segment
62 /// - 8 bits for column number.
64 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
66 public struct Location {
70 public readonly int LineOffset;
71 public readonly int File;
73 public Checkpoint (int file, int line)
76 LineOffset = line - (int) (line % (1 << line_delta_bits));
80 static ArrayList source_list;
81 static Hashtable source_files;
82 static int checkpoint_bits;
83 static int source_count;
84 static int current_source;
85 static int line_delta_bits;
86 static int line_delta_mask;
87 static int column_bits;
88 static int column_mask;
89 static Checkpoint [] checkpoints;
90 static int checkpoint_index;
92 public readonly static Location Null = new Location (-1);
93 public static bool InEmacs;
97 source_files = new Hashtable ();
98 source_list = new ArrayList ();
100 checkpoints = new Checkpoint [10];
103 public static void Reset ()
105 source_files = new Hashtable ();
106 source_list = new ArrayList ();
112 // This must be called before parsing/tokenizing any files.
114 static public void AddFile (string name)
116 string path = Path.GetFullPath (name);
118 if (source_files.Contains (path)){
119 int id = (int) source_files [path];
120 string other_name = ((SourceFile) source_list [id - 1]).Name;
121 if (name.Equals (other_name))
122 Report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
124 Report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
128 source_files.Add (path, ++source_count);
129 source_list.Add (new SourceFile (name, path, source_count));
132 static public SourceFile[] SourceFiles {
134 SourceFile[] retval = new SourceFile [source_list.Count];
135 source_list.CopyTo (retval, 0);
141 // After adding all source files we want to compile with AddFile(), this method
142 // must be called to `reserve' an appropriate number of bits in the token for the
143 // source file. We reserve some extra space for files we encounter via #line
144 // directives while parsing.
146 static public void Initialize ()
148 checkpoints = new Checkpoint [source_list.Count * 2];
149 if (checkpoints.Length > 0)
150 checkpoints [0] = new Checkpoint (0, 0);
155 line_delta_mask = 0xFF00;
156 checkpoint_index = 0;
157 checkpoint_bits = 16;
161 // This is used when we encounter a #line preprocessing directive.
163 static public SourceFile LookupFile (string name)
165 string path = name.Length == 0 ? "" : Path.GetFullPath (name);
167 if (!source_files.Contains (path)) {
168 if (source_count >= (1 << checkpoint_bits))
169 return new SourceFile (name, path, 0);
171 source_files.Add (path, ++source_count);
172 SourceFile retval = new SourceFile (name, path, source_count);
173 source_list.Add (retval);
177 int index = (int) source_files [path];
178 return (SourceFile) source_list [index - 1];
181 static public void Push (SourceFile file)
183 current_source = file.Index;
184 // File is always pushed before being changed.
188 // If we're compiling with debugging support, this is called between parsing
189 // and code generation to register all the source files with the
192 static public void DefineSymbolDocuments (SymbolWriter symwriter)
194 foreach (SourceFile file in source_list) {
195 file.SourceFileEntry = symwriter.DefineDocument (file.Path);
199 public Location (int row)
204 public Location (int row, int column)
213 int max = checkpoint_index < 10 ?
214 checkpoint_index : 10;
215 for (int i = 0; i < max; i++) {
216 int offset = checkpoints [checkpoint_index - i].LineOffset;
217 delta = row - offset;
219 delta < (1 << line_delta_bits) &&
220 checkpoints [checkpoint_index - i].File == current_source) {
221 target = checkpoint_index - i;
226 AddCheckpoint (current_source, row);
227 target = checkpoint_index;
228 delta = row % (1 << line_delta_bits);
231 (long) (delta << column_bits) +
232 (long) (target << (line_delta_bits + column_bits));
233 token = l > 0xFFFFFFFF ? 0 : (int) l;
237 static void AddCheckpoint (int file, int row)
239 if (checkpoints.Length == ++checkpoint_index) {
240 Checkpoint [] tmp = new Checkpoint [checkpoint_index * 2];
241 Array.Copy (checkpoints, tmp, checkpoints.Length);
244 checkpoints [checkpoint_index] = new Checkpoint (file, row);
247 public override string ToString ()
249 if (column_bits == 0 || InEmacs)
250 return Name + "(" + Row + "):";
252 return Name + "(" + Row + "," + Column +
253 (Column == column_mask ? "+):" : "):");
257 /// Whether the Location is Null
260 get { return token == 0; }
266 if (token == 0 || index == 0)
269 SourceFile file = (SourceFile) source_list [index - 1];
274 int CheckpointIndex {
275 get { return (int) ((token & 0xFFFF0000) >> (line_delta_bits + column_bits)); }
282 return checkpoints [CheckpointIndex].LineOffset + ((token & line_delta_mask) >> column_bits);
290 return (int) (token & column_mask);
298 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));
299 return checkpoints [CheckpointIndex].File;
303 // The ISymbolDocumentWriter interface is used by the symbol writer to
304 // describe a single source file - for each source file there's exactly
305 // one corresponding ISymbolDocumentWriter instance.
307 // This class has an internal hash table mapping source document names
308 // to such ISymbolDocumentWriter instances - so there's exactly one
309 // instance per document.
311 // This property returns the ISymbolDocumentWriter instance which belongs
312 // to the location's source file.
314 // If we don't have a symbol writer, this property is always null.
315 public SourceFile SourceFile {
320 return (SourceFile) source_list [index - 1];
325 public class LocatedToken
327 public readonly Location Location;
328 public readonly string Value;
330 public LocatedToken (Location loc, string value)
336 public override string ToString ()
338 return Location.ToString () + Value;