2 // location.cs: Keeps track of the location of source code entity
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright 2001 Ximian, Inc.
9 // Copyright 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 class SourceFile : ISourceFile {
26 public readonly string Name;
27 public readonly string Path;
28 public readonly int Index;
29 public bool AutoGenerated;
30 public bool IsIncludeFile;
33 byte[] guid, checksum;
35 public SourceFile (string name, string path, int index, bool is_include)
40 this.IsIncludeFile = is_include;
43 public SourceFileEntry SourceFileEntry {
47 SourceFileEntry ISourceFile.Entry {
51 public void SetChecksum (byte[] guid, byte[] checksum)
54 this.checksum = checksum;
57 public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter)
60 file = symwriter.DefineDocument (Path, guid, checksum);
62 file = symwriter.DefineDocument (Path);
64 file.SetAutoGenerated ();
68 public override string ToString ()
70 return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
71 Name, Path, Index, SourceFileEntry);
75 public class CompilationUnit : SourceFile, ICompileUnit
77 CompileUnitEntry comp_unit;
78 Hashtable include_files;
80 public CompilationUnit (string name, string path, int index)
81 : base (name, path, index, false)
84 public void AddFile (SourceFile file)
86 if (include_files == null)
87 include_files = new Hashtable ();
89 if (!include_files.Contains (file.Path))
90 include_files.Add (file.Path, file);
93 CompileUnitEntry ICompileUnit.Entry {
94 get { return comp_unit; }
97 public CompileUnitEntry CompileUnitEntry {
98 get { return comp_unit; }
101 public override void DefineSymbolInfo (MonoSymbolWriter symwriter)
103 base.DefineSymbolInfo (symwriter);
105 comp_unit = symwriter.DefineCompilationUnit (SourceFileEntry);
107 if (include_files != null) {
108 foreach (SourceFile include in include_files.Values) {
109 include.DefineSymbolInfo (symwriter);
110 comp_unit.AddFile (include.SourceFileEntry);
117 /// Keeps track of the location in the program
121 /// This uses a compact representation and a couple of auxiliary
122 /// structures to keep track of tokens to (file,line and column)
123 /// mappings. The usage of the bits is:
125 /// - 16 bits for "checkpoint" which is a mixed concept of
126 /// file and "line segment"
127 /// - 8 bits for line delta (offset) from the line segment
128 /// - 8 bits for column number.
130 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
132 public struct Location {
136 public readonly int LineOffset;
137 public readonly int CompilationUnit;
138 public readonly int File;
140 public Checkpoint (int compile_unit, int file, int line)
143 CompilationUnit = compile_unit;
144 LineOffset = line - (int) (line % (1 << line_delta_bits));
148 static ArrayList source_list;
149 static ArrayList compile_units;
150 static Hashtable source_files;
151 static int checkpoint_bits;
152 static int source_count;
153 static int current_source;
154 static int current_compile_unit;
155 static int line_delta_bits;
156 static int line_delta_mask;
157 static int column_bits;
158 static int column_mask;
159 static Checkpoint [] checkpoints;
160 static int checkpoint_index;
162 public readonly static Location Null = new Location (-1);
163 public static bool InEmacs;
167 source_files = new Hashtable ();
168 source_list = new ArrayList ();
169 compile_units = new ArrayList ();
171 current_compile_unit = 0;
172 checkpoints = new Checkpoint [10];
175 public static void Reset ()
177 source_files = new Hashtable ();
178 source_list = new ArrayList ();
179 compile_units = new ArrayList ();
181 current_compile_unit = 0;
186 // This must be called before parsing/tokenizing any files.
188 static public void AddFile (string name)
190 string path = Path.GetFullPath (name);
192 if (source_files.Contains (path)){
193 int id = (int) source_files [path];
194 string other_name = ((SourceFile) source_list [id - 1]).Name;
195 if (name.Equals (other_name))
196 Report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
198 Report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
202 source_files.Add (path, ++source_count);
203 CompilationUnit unit = new CompilationUnit (name, path, source_count);
204 source_list.Add (unit);
205 compile_units.Add (unit);
208 static public CompilationUnit[] SourceFiles {
210 CompilationUnit[] retval = new CompilationUnit [compile_units.Count];
211 compile_units.CopyTo (retval, 0);
217 // After adding all source files we want to compile with AddFile(), this method
218 // must be called to `reserve' an appropriate number of bits in the token for the
219 // source file. We reserve some extra space for files we encounter via #line
220 // directives while parsing.
222 static public void Initialize ()
224 checkpoints = new Checkpoint [source_list.Count * 2];
225 if (checkpoints.Length > 0)
226 checkpoints [0] = new Checkpoint (0, 0, 0);
231 line_delta_mask = 0xFF00;
232 checkpoint_index = 0;
233 checkpoint_bits = 16;
237 // This is used when we encounter a #line preprocessing directive.
239 static public SourceFile LookupFile (CompilationUnit comp_unit, string name)
242 if (!Path.IsPathRooted (name)) {
243 string root = Path.GetDirectoryName (comp_unit.Path);
244 path = Path.Combine (root, name);
248 if (!source_files.Contains (path)) {
249 if (source_count >= (1 << checkpoint_bits))
250 return new SourceFile (name, path, 0, true);
252 source_files.Add (path, ++source_count);
253 SourceFile retval = new SourceFile (name, path, source_count, true);
254 source_list.Add (retval);
258 int index = (int) source_files [path];
259 return (SourceFile) source_list [index - 1];
262 static public void Push (CompilationUnit compile_unit, SourceFile file)
264 current_source = file != null ? file.Index : -1;
265 current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
266 // File is always pushed before being changed.
270 // If we're compiling with debugging support, this is called between parsing
271 // and code generation to register all the source files with the
274 static public void DefineSymbolDocuments (MonoSymbolWriter symwriter)
276 foreach (CompilationUnit unit in compile_units)
277 unit.DefineSymbolInfo (symwriter);
280 public Location (int row)
285 public Location (int row, int column)
296 int max = checkpoint_index < 10 ?
297 checkpoint_index : 10;
298 for (int i = 0; i < max; i++) {
299 int offset = checkpoints [checkpoint_index - i].LineOffset;
300 delta = row - offset;
302 delta < (1 << line_delta_bits) &&
303 checkpoints [checkpoint_index - i].File == current_source) {
304 target = checkpoint_index - i;
309 AddCheckpoint (current_compile_unit, current_source, row);
310 target = checkpoint_index;
311 delta = row % (1 << line_delta_bits);
314 (long) (delta << column_bits) +
315 (long) (target << (line_delta_bits + column_bits));
316 token = l > 0xFFFFFFFF ? 0 : (int) l;
320 static void AddCheckpoint (int compile_unit, int file, int row)
322 if (checkpoints.Length == ++checkpoint_index) {
323 Checkpoint [] tmp = new Checkpoint [checkpoint_index * 2];
324 Array.Copy (checkpoints, tmp, checkpoints.Length);
327 checkpoints [checkpoint_index] = new Checkpoint (compile_unit, file, row);
330 public override string ToString ()
332 if (column_bits == 0 || InEmacs)
333 return Name + "(" + Row.ToString () + "):";
335 return Name + "(" + Row.ToString () + "," + Column.ToString () +
336 (Column == column_mask ? "+):" : "):");
340 /// Whether the Location is Null
343 get { return token == 0; }
349 if (token == 0 || index == 0)
352 SourceFile file = (SourceFile) source_list [index - 1];
357 int CheckpointIndex {
358 get { return (int) ((token & 0xFFFF0000) >> (line_delta_bits + column_bits)); }
365 return checkpoints [CheckpointIndex].LineOffset + ((token & line_delta_mask) >> column_bits);
373 int col = (int) (token & column_mask);
374 return col == 255 ? 1 : col;
380 return (int) (token & column_mask) == 255;
384 public int CompilationUnitIndex {
388 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));
389 return checkpoints [CheckpointIndex].CompilationUnit;
397 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));
398 return checkpoints [CheckpointIndex].File;
402 // The ISymbolDocumentWriter interface is used by the symbol writer to
403 // describe a single source file - for each source file there's exactly
404 // one corresponding ISymbolDocumentWriter instance.
406 // This class has an internal hash table mapping source document names
407 // to such ISymbolDocumentWriter instances - so there's exactly one
408 // instance per document.
410 // This property returns the ISymbolDocumentWriter instance which belongs
411 // to the location's source file.
413 // If we don't have a symbol writer, this property is always null.
414 public SourceFile SourceFile {
419 return (SourceFile) source_list [index - 1];
423 public CompilationUnit CompilationUnit {
425 int index = CompilationUnitIndex;
428 return (CompilationUnit) source_list [index - 1];
433 public class LocatedToken
435 public readonly Location Location;
436 public readonly string Value;
438 public LocatedToken (Location loc, string value)
444 public override string ToString ()
446 return Location.ToString () + Value;