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;
23 // This is one single source file.
25 public class SourceFile : IEquatable<SourceFile>
28 // Used by #line directive to track hidden sequence point
31 struct LocationRegion : IComparable<LocationRegion>
33 public readonly Location Start;
34 public readonly Location End;
36 public LocationRegion (Location start, Location end)
42 public int CompareTo (LocationRegion other)
44 if (Start.Row == other.Start.Row)
45 return Start.Column.CompareTo (other.Start.Column);
47 return Start.Row.CompareTo (other.Start.Row);
50 public override string ToString ()
52 return Start.ToString () + " - " + End.ToString ();
56 static readonly byte[] MD5Algorith = { 96, 166, 110, 64, 207, 100, 130, 76, 182, 240, 66, 212, 129, 114, 167, 153 };
58 public readonly string Name;
59 public readonly string OriginalFullPathName;
60 public readonly int Index;
61 public bool AutoGenerated;
62 public Func<SourceFile, SeekableStreamReader> GetInputStream;
65 byte[] algGuid, checksum;
66 List<LocationRegion> hidden_lines;
68 public SourceFile (string name, string path, int index)
72 this.OriginalFullPathName = path;
75 public SourceFile (string name, string path, int index, Func<SourceFile, SeekableStreamReader> inputStreamDelegate) : this (name, path, index)
77 this.GetInputStream = inputStreamDelegate;
80 public byte[] Checksum {
86 public bool HasChecksum {
88 return checksum != null;
92 public SourceFileEntry SourceFileEntry {
98 public void SetChecksum (byte[] checksum)
100 SetChecksum (MD5Algorith, checksum);
103 public void SetChecksum (byte[] algorithmGuid, byte[] checksum)
105 this.algGuid = algorithmGuid;
106 this.checksum = checksum;
109 public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter, List<KeyValuePair<string, string>> pathMap)
111 if (hidden_lines != null)
112 hidden_lines.Sort ();
114 file = new SourceFileEntry (symwriter, GetFullPathName (pathMap), OriginalFullPathName, algGuid, checksum);
116 file.SetAutoGenerated ();
121 public string GetFullPathName (List<KeyValuePair<string, string>> pathMap)
123 var path = OriginalFullPathName;
124 if (pathMap != null) {
125 foreach (var map in pathMap) {
126 var prefix = map.Key;
127 if (path.Length <= prefix.Length)
130 if (path [prefix.Length] != Path.DirectorySeparatorChar)
133 if (!path.StartsWith (prefix, StringComparison.Ordinal))
136 path = map.Value + path.Substring (prefix.Length);
143 public bool Equals (SourceFile other)
145 return OriginalFullPathName == other.OriginalFullPathName;
148 public bool IsHiddenLocation (Location loc)
150 if (hidden_lines == null)
153 int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
156 var found = hidden_lines[index - 1];
157 if (loc.Row < found.End.Row)
164 public void RegisterHiddenScope (Location start, Location end)
166 if (hidden_lines == null)
167 hidden_lines = new List<LocationRegion> ();
169 hidden_lines.Add (new LocationRegion (start, end));
172 public override string ToString ()
174 return String.Format ("SourceFile ({0}:{1}:{2})", Name, OriginalFullPathName, Index);
179 /// Keeps track of the location in the program
183 /// This uses a compact representation and a couple of auxiliary
184 /// structures to keep track of tokens to (file,line and column)
185 /// mappings. The usage of the bits is:
187 /// - 16 bits for "checkpoint" which is a mixed concept of
188 /// file and "line segment"
189 /// - 8 bits for line delta (offset) from the line segment
190 /// - 8 bits for column number.
192 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
194 public struct Location : IEquatable<Location>
197 public readonly int LineOffset;
198 public readonly int File;
200 public Checkpoint (int file, int line)
203 LineOffset = line - (int) (line % (1 << line_delta_bits));
210 const int column_bits = 24;
211 const int line_delta_bits = 24;
215 const int column_bits = 8;
216 const int line_delta_bits = 8;
218 const int checkpoint_bits = 16;
220 const int column_mask = (1 << column_bits) - 1;
221 const int max_column = column_mask;
223 static List<SourceFile> source_list;
224 static Checkpoint [] checkpoints;
225 static int checkpoint_index;
227 public readonly static Location Null = new Location ();
228 public static bool InEmacs;
235 public static void Reset ()
237 source_list = new List<SourceFile> ();
238 checkpoint_index = 0;
241 public static void AddFile (SourceFile file)
243 source_list.Add (file);
247 // After adding all source files we want to compile with AddFile(), this method
248 // must be called to `reserve' an appropriate number of bits in the token for the
249 // source file. We reserve some extra space for files we encounter via #line
250 // directives while parsing.
252 static public void Initialize (List<SourceFile> files)
254 source_list.AddRange (files);
256 checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
257 if (checkpoints.Length > 0)
258 checkpoints [0] = new Checkpoint (0, 0);
261 public Location (SourceFile file, int row, int column)
266 if (column > max_column)
272 // TODO: For eval only, need better handling of empty
273 int file_index = file == null ? 0 : file.Index;
275 // FIXME: This value is certainly wrong but what was the intension
276 int max = checkpoint_index < 10 ?
277 checkpoint_index : 10;
278 for (int i = 0; i < max; i++) {
279 int offset = checkpoints [checkpoint_index - i].LineOffset;
280 delta = row - offset;
282 delta < (1 << line_delta_bits) &&
283 checkpoints[checkpoint_index - i].File == file_index) {
284 target = checkpoint_index - i;
289 AddCheckpoint (file_index, row);
290 target = checkpoint_index;
291 delta = row % (1 << line_delta_bits);
295 (delta << column_bits) +
296 (target << (line_delta_bits + column_bits));
300 token = l > 0xFFFFFFFF ? 0 : (int) l;
305 public static Location operator - (Location loc, int columns)
307 return new Location (loc.SourceFile, loc.Row, loc.Column - columns);
310 static void AddCheckpoint (int file, int row)
312 if (checkpoints.Length == ++checkpoint_index) {
313 Array.Resize (ref checkpoints, checkpoint_index * 2);
315 checkpoints [checkpoint_index] = new Checkpoint (file, row);
318 string FormatLocation (string fileName)
320 if (column_bits == 0 || InEmacs)
321 return fileName + "(" + Row.ToString () + "):";
323 return fileName + "(" + Row.ToString () + "," + Column.ToString () +
324 (Column == max_column ? "+):" : "):");
327 public override string ToString ()
329 return FormatLocation (Name);
332 public string ToStringFullName ()
334 return FormatLocation (NameFullPath);
338 /// Whether the Location is Null
341 get { return token == 0; }
347 if (token == 0 || index <= 0)
350 SourceFile file = source_list [index - 1];
355 public string NameFullPath {
361 return source_list[index - 1].OriginalFullPathName;
365 int CheckpointIndex {
367 const int checkpoint_mask = (1 << checkpoint_bits) - 1;
368 return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
377 int offset = checkpoints[CheckpointIndex].LineOffset;
379 const int line_delta_mask = (1 << column_bits) - 1;
380 return offset + (((int)(token >> column_bits)) & line_delta_mask);
388 return (int) (token & column_mask);
396 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));
397 return checkpoints [CheckpointIndex].File;
401 public SourceFile SourceFile {
406 return source_list [index - 1];
410 #region IEquatable<Location> Members
412 public bool Equals (Location other)
414 return this.token == other.token;
421 // A bag of additional locations to support full ast tree
423 public class LocationsBag
425 public class MemberLocations
427 public readonly IList<Tuple<Modifiers, Location>> Modifiers;
428 List<Location> locations;
430 public MemberLocations (IList<Tuple<Modifiers, Location>> mods)
435 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location loc)
441 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
447 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, List<Location> locs)
455 public Location this [int index] {
457 return locations [index];
463 return locations.Count;
469 public void AddLocations (Location loc)
471 if (locations == null) {
472 locations = new List<Location> ();
478 public void AddLocations (params Location[] additional)
480 if (locations == null) {
481 locations = new List<Location> (additional);
483 locations.AddRange (additional);
488 Dictionary<object, List<Location>> simple_locs = new Dictionary<object, List<Location>> (ReferenceEquality<object>.Default);
489 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
491 [Conditional ("FULL_AST")]
492 public void AddLocation (object element, params Location[] locations)
494 simple_locs.Add (element, new List<Location> (locations));
497 [Conditional ("FULL_AST")]
498 public void InsertLocation (object element, int index, Location location)
500 List<Location> found;
501 if (!simple_locs.TryGetValue (element, out found)) {
502 found = new List<Location> ();
503 simple_locs.Add (element, found);
506 found.Insert (index, location);
509 [Conditional ("FULL_AST")]
510 public void AddStatement (object element, params Location[] locations)
512 if (locations.Length == 0)
513 throw new ArgumentException ("Statement is missing semicolon location");
515 AddLocation (element, locations);
518 [Conditional ("FULL_AST")]
519 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations)
521 member_locs.Add (member, new MemberLocations (modLocations));
524 [Conditional ("FULL_AST")]
525 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, Location location)
527 member_locs.Add (member, new MemberLocations (modLocations, location));
530 [Conditional ("FULL_AST")]
531 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
533 member_locs.Add (member, new MemberLocations (modLocations, locations));
536 [Conditional ("FULL_AST")]
537 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, List<Location> locations)
539 member_locs.Add (member, new MemberLocations (modLocations, locations));
542 [Conditional ("FULL_AST")]
543 public void AppendTo (object element, Location location)
545 List<Location> found;
546 if (!simple_locs.TryGetValue (element, out found)) {
547 found = new List<Location> ();
548 simple_locs.Add (element, found);
551 found.Add (location);
554 [Conditional ("FULL_AST")]
555 public void AppendToMember (MemberCore existing, params Location[] locations)
557 MemberLocations member;
558 if (member_locs.TryGetValue (existing, out member)) {
559 member.AddLocations (locations);
564 public List<Location> GetLocations (object element)
566 List<Location> found;
567 simple_locs.TryGetValue (element, out found);
571 public MemberLocations GetMemberLocation (MemberCore element)
573 MemberLocations found;
574 member_locs.TryGetValue (element, out found);