2 // location.cs: Keeps track of the location of source code entity
6 // Atsushi Enomoto <atsushi@ximian.com>
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright 2001 Ximian, Inc.
10 // Copyright 2005 Novell, Inc.
14 using System.Collections.Generic;
15 using Mono.CompilerServices.SymbolWriter;
16 using System.Diagnostics;
22 // This is one single source file.
24 public class SourceFile : IEquatable<SourceFile>
27 // Used by #line directive to track hidden sequence point
30 struct LocationRegion : IComparable<LocationRegion>
32 public readonly Location Start;
33 public readonly Location End;
35 public LocationRegion (Location start, Location end)
41 public int CompareTo (LocationRegion other)
43 if (Start.Row == other.Start.Row)
44 return Start.Column.CompareTo (other.Start.Column);
46 return Start.Row.CompareTo (other.Start.Row);
49 public override string ToString ()
51 return Start.ToString () + " - " + End.ToString ();
55 static readonly byte[] MD5Algorith = { 96, 166, 110, 64, 207, 100, 130, 76, 182, 240, 66, 212, 129, 114, 167, 153 };
57 public readonly string Name;
58 public readonly string FullPathName;
59 public readonly int Index;
60 public bool AutoGenerated;
63 byte[] algGuid, checksum;
64 List<LocationRegion> hidden_lines;
66 public SourceFile (string name, string path, int index)
70 this.FullPathName = path;
73 public byte[] Checksum {
79 public bool HasChecksum {
81 return checksum != null;
85 public SourceFileEntry SourceFileEntry {
91 public void SetChecksum (byte[] checksum)
93 SetChecksum (MD5Algorith, checksum);
96 public void SetChecksum (byte[] algorithmGuid, byte[] checksum)
98 this.algGuid = algorithmGuid;
99 this.checksum = checksum;
102 public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter)
104 if (hidden_lines != null)
105 hidden_lines.Sort ();
107 file = new SourceFileEntry (symwriter, FullPathName, algGuid, checksum);
109 file.SetAutoGenerated ();
114 public bool Equals (SourceFile other)
116 return FullPathName == other.FullPathName;
119 public bool IsHiddenLocation (Location loc)
121 if (hidden_lines == null)
124 int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
127 var found = hidden_lines[index - 1];
128 if (loc.Row < found.End.Row)
135 public void RegisterHiddenScope (Location start, Location end)
137 if (hidden_lines == null)
138 hidden_lines = new List<LocationRegion> ();
140 hidden_lines.Add (new LocationRegion (start, end));
143 public override string ToString ()
145 return String.Format ("SourceFile ({0}:{1}:{2})", Name, FullPathName, Index);
150 /// Keeps track of the location in the program
154 /// This uses a compact representation and a couple of auxiliary
155 /// structures to keep track of tokens to (file,line and column)
156 /// mappings. The usage of the bits is:
158 /// - 16 bits for "checkpoint" which is a mixed concept of
159 /// file and "line segment"
160 /// - 8 bits for line delta (offset) from the line segment
161 /// - 8 bits for column number.
163 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
165 public struct Location : IEquatable<Location>
168 public readonly int LineOffset;
169 public readonly int File;
171 public Checkpoint (int file, int line)
174 LineOffset = line - (int) (line % (1 << line_delta_bits));
181 const int column_bits = 24;
182 const int line_delta_bits = 24;
186 const int column_bits = 8;
187 const int line_delta_bits = 8;
189 const int checkpoint_bits = 16;
191 const int column_mask = (1 << column_bits) - 1;
192 const int max_column = column_mask;
194 static List<SourceFile> source_list;
195 static Checkpoint [] checkpoints;
196 static int checkpoint_index;
198 public readonly static Location Null = new Location ();
199 public static bool InEmacs;
206 public static void Reset ()
208 source_list = new List<SourceFile> ();
209 checkpoint_index = 0;
212 public static void AddFile (SourceFile file)
214 source_list.Add (file);
218 // After adding all source files we want to compile with AddFile(), this method
219 // must be called to `reserve' an appropriate number of bits in the token for the
220 // source file. We reserve some extra space for files we encounter via #line
221 // directives while parsing.
223 static public void Initialize (List<SourceFile> files)
225 source_list.AddRange (files);
227 checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
228 if (checkpoints.Length > 0)
229 checkpoints [0] = new Checkpoint (0, 0);
232 public Location (SourceFile file, int row, int column)
237 if (column > max_column)
243 // TODO: For eval only, need better handling of empty
244 int file_index = file == null ? 0 : file.Index;
246 // FIXME: This value is certainly wrong but what was the intension
247 int max = checkpoint_index < 10 ?
248 checkpoint_index : 10;
249 for (int i = 0; i < max; i++) {
250 int offset = checkpoints [checkpoint_index - i].LineOffset;
251 delta = row - offset;
253 delta < (1 << line_delta_bits) &&
254 checkpoints[checkpoint_index - i].File == file_index) {
255 target = checkpoint_index - i;
260 AddCheckpoint (file_index, row);
261 target = checkpoint_index;
262 delta = row % (1 << line_delta_bits);
266 (delta << column_bits) +
267 (target << (line_delta_bits + column_bits));
271 token = l > 0xFFFFFFFF ? 0 : (int) l;
276 public static Location operator - (Location loc, int columns)
278 return new Location (loc.SourceFile, loc.Row, loc.Column - columns);
281 static void AddCheckpoint (int file, int row)
283 if (checkpoints.Length == ++checkpoint_index) {
284 Array.Resize (ref checkpoints, checkpoint_index * 2);
286 checkpoints [checkpoint_index] = new Checkpoint (file, row);
289 string FormatLocation (string fileName)
291 if (column_bits == 0 || InEmacs)
292 return fileName + "(" + Row.ToString () + "):";
294 return fileName + "(" + Row.ToString () + "," + Column.ToString () +
295 (Column == max_column ? "+):" : "):");
298 public override string ToString ()
300 return FormatLocation (Name);
303 public string ToStringFullName ()
305 return FormatLocation (NameFullPath);
309 /// Whether the Location is Null
312 get { return token == 0; }
318 if (token == 0 || index <= 0)
321 SourceFile file = source_list [index - 1];
326 public string NameFullPath {
329 if (token == 0 || index <= 0)
332 return source_list[index - 1].FullPathName;
336 int CheckpointIndex {
338 const int checkpoint_mask = (1 << checkpoint_bits) - 1;
339 return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
348 int offset = checkpoints[CheckpointIndex].LineOffset;
350 const int line_delta_mask = (1 << column_bits) - 1;
351 return offset + (((int)(token >> column_bits)) & line_delta_mask);
359 return (int) (token & column_mask);
367 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));
368 return checkpoints [CheckpointIndex].File;
372 // The ISymbolDocumentWriter interface is used by the symbol writer to
373 // describe a single source file - for each source file there's exactly
374 // one corresponding ISymbolDocumentWriter instance.
376 // This class has an internal hash table mapping source document names
377 // to such ISymbolDocumentWriter instances - so there's exactly one
378 // instance per document.
380 // This property returns the ISymbolDocumentWriter instance which belongs
381 // to the location's source file.
383 // If we don't have a symbol writer, this property is always null.
384 public SourceFile SourceFile {
389 return source_list [index - 1];
393 #region IEquatable<Location> Members
395 public bool Equals (Location other)
397 return this.token == other.token;
404 // A bag of additional locations to support full ast tree
406 public class LocationsBag
408 public class MemberLocations
410 public readonly IList<Tuple<Modifiers, Location>> Modifiers;
411 List<Location> locations;
413 public MemberLocations (IList<Tuple<Modifiers, Location>> mods)
418 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location loc)
424 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
430 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, List<Location> locs)
438 public Location this [int index] {
440 return locations [index];
446 return locations.Count;
452 public void AddLocations (Location loc)
454 if (locations == null) {
455 locations = new List<Location> ();
461 public void AddLocations (params Location[] additional)
463 if (locations == null) {
464 locations = new List<Location> (additional);
466 locations.AddRange (additional);
471 Dictionary<object, List<Location>> simple_locs = new Dictionary<object, List<Location>> (ReferenceEquality<object>.Default);
472 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
474 [Conditional ("FULL_AST")]
475 public void AddLocation (object element, params Location[] locations)
477 simple_locs.Add (element, new List<Location> (locations));
480 [Conditional ("FULL_AST")]
481 public void InsertLocation (object element, int index, Location location)
483 List<Location> found;
484 if (!simple_locs.TryGetValue (element, out found)) {
485 found = new List<Location> ();
486 simple_locs.Add (element, found);
489 found.Insert (index, location);
492 [Conditional ("FULL_AST")]
493 public void AddStatement (object element, params Location[] locations)
495 if (locations.Length == 0)
496 throw new ArgumentException ("Statement is missing semicolon location");
498 AddLocation (element, locations);
501 [Conditional ("FULL_AST")]
502 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations)
504 member_locs.Add (member, new MemberLocations (modLocations));
507 [Conditional ("FULL_AST")]
508 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, Location location)
510 member_locs.Add (member, new MemberLocations (modLocations, location));
513 [Conditional ("FULL_AST")]
514 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
516 member_locs.Add (member, new MemberLocations (modLocations, locations));
519 [Conditional ("FULL_AST")]
520 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, List<Location> locations)
522 member_locs.Add (member, new MemberLocations (modLocations, locations));
525 [Conditional ("FULL_AST")]
526 public void AppendTo (object element, Location location)
528 List<Location> found;
529 if (!simple_locs.TryGetValue (element, out found)) {
530 found = new List<Location> ();
531 simple_locs.Add (element, found);
534 found.Add (location);
537 [Conditional ("FULL_AST")]
538 public void AppendToMember (MemberCore existing, params Location[] locations)
540 MemberLocations member;
541 if (member_locs.TryGetValue (existing, out member)) {
542 member.AddLocations (locations);
547 public List<Location> GetLocations (object element)
549 List<Location> found;
550 simple_locs.TryGetValue (element, out found);
554 public MemberLocations GetMemberLocation (MemberCore element)
556 MemberLocations found;
557 member_locs.TryGetValue (element, out found);