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;
64 byte[] algGuid, checksum;
65 List<LocationRegion> hidden_lines;
67 public SourceFile (string name, string path, int index)
71 this.OriginalFullPathName = path;
74 public byte[] Checksum {
80 public bool HasChecksum {
82 return checksum != null;
86 public SourceFileEntry SourceFileEntry {
92 public void SetChecksum (byte[] checksum)
94 SetChecksum (MD5Algorith, checksum);
97 public void SetChecksum (byte[] algorithmGuid, byte[] checksum)
99 this.algGuid = algorithmGuid;
100 this.checksum = checksum;
103 public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter, List<KeyValuePair<string, string>> pathMap)
105 if (hidden_lines != null)
106 hidden_lines.Sort ();
108 file = new SourceFileEntry (symwriter, GetFullPathName (pathMap), OriginalFullPathName, algGuid, checksum);
110 file.SetAutoGenerated ();
115 public string GetFullPathName (List<KeyValuePair<string, string>> pathMap)
117 var path = OriginalFullPathName;
118 if (pathMap != null) {
119 foreach (var map in pathMap) {
120 var prefix = map.Key;
121 if (path.Length <= prefix.Length)
124 if (path [prefix.Length] != Path.DirectorySeparatorChar)
127 if (!path.StartsWith (prefix, StringComparison.Ordinal))
130 path = map.Value + path.Substring (prefix.Length);
137 public bool Equals (SourceFile other)
139 return OriginalFullPathName == other.OriginalFullPathName;
142 public bool IsHiddenLocation (Location loc)
144 if (hidden_lines == null)
147 int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
150 var found = hidden_lines[index - 1];
151 if (loc.Row < found.End.Row)
158 public void RegisterHiddenScope (Location start, Location end)
160 if (hidden_lines == null)
161 hidden_lines = new List<LocationRegion> ();
163 hidden_lines.Add (new LocationRegion (start, end));
166 public override string ToString ()
168 return String.Format ("SourceFile ({0}:{1}:{2})", Name, OriginalFullPathName, Index);
173 /// Keeps track of the location in the program
177 /// This uses a compact representation and a couple of auxiliary
178 /// structures to keep track of tokens to (file,line and column)
179 /// mappings. The usage of the bits is:
181 /// - 16 bits for "checkpoint" which is a mixed concept of
182 /// file and "line segment"
183 /// - 8 bits for line delta (offset) from the line segment
184 /// - 8 bits for column number.
186 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
188 public struct Location : IEquatable<Location>
191 public readonly int LineOffset;
192 public readonly int File;
194 public Checkpoint (int file, int line)
197 LineOffset = line - (int) (line % (1 << line_delta_bits));
204 const int column_bits = 24;
205 const int line_delta_bits = 24;
209 const int column_bits = 8;
210 const int line_delta_bits = 8;
212 const int checkpoint_bits = 16;
214 const int column_mask = (1 << column_bits) - 1;
215 const int max_column = column_mask;
217 static List<SourceFile> source_list;
218 static Checkpoint [] checkpoints;
219 static int checkpoint_index;
221 public readonly static Location Null = new Location ();
222 public static bool InEmacs;
229 public static void Reset ()
231 source_list = new List<SourceFile> ();
232 checkpoint_index = 0;
235 public static void AddFile (SourceFile file)
237 source_list.Add (file);
241 // After adding all source files we want to compile with AddFile(), this method
242 // must be called to `reserve' an appropriate number of bits in the token for the
243 // source file. We reserve some extra space for files we encounter via #line
244 // directives while parsing.
246 static public void Initialize (List<SourceFile> files)
248 source_list.AddRange (files);
250 checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
251 if (checkpoints.Length > 0)
252 checkpoints [0] = new Checkpoint (0, 0);
255 public Location (SourceFile file, int row, int column)
260 if (column > max_column)
266 // TODO: For eval only, need better handling of empty
267 int file_index = file == null ? 0 : file.Index;
269 // FIXME: This value is certainly wrong but what was the intension
270 int max = checkpoint_index < 10 ?
271 checkpoint_index : 10;
272 for (int i = 0; i < max; i++) {
273 int offset = checkpoints [checkpoint_index - i].LineOffset;
274 delta = row - offset;
276 delta < (1 << line_delta_bits) &&
277 checkpoints[checkpoint_index - i].File == file_index) {
278 target = checkpoint_index - i;
283 AddCheckpoint (file_index, row);
284 target = checkpoint_index;
285 delta = row % (1 << line_delta_bits);
289 (delta << column_bits) +
290 (target << (line_delta_bits + column_bits));
294 token = l > 0xFFFFFFFF ? 0 : (int) l;
299 public static Location operator - (Location loc, int columns)
301 return new Location (loc.SourceFile, loc.Row, loc.Column - columns);
304 static void AddCheckpoint (int file, int row)
306 if (checkpoints.Length == ++checkpoint_index) {
307 Array.Resize (ref checkpoints, checkpoint_index * 2);
309 checkpoints [checkpoint_index] = new Checkpoint (file, row);
312 string FormatLocation (string fileName)
314 if (column_bits == 0 || InEmacs)
315 return fileName + "(" + Row.ToString () + "):";
317 return fileName + "(" + Row.ToString () + "," + Column.ToString () +
318 (Column == max_column ? "+):" : "):");
321 public override string ToString ()
323 return FormatLocation (Name);
326 public string ToStringFullName ()
328 return FormatLocation (NameFullPath);
332 /// Whether the Location is Null
335 get { return token == 0; }
341 if (token == 0 || index <= 0)
344 SourceFile file = source_list [index - 1];
349 public string NameFullPath {
355 return source_list[index - 1].OriginalFullPathName;
359 int CheckpointIndex {
361 const int checkpoint_mask = (1 << checkpoint_bits) - 1;
362 return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
371 int offset = checkpoints[CheckpointIndex].LineOffset;
373 const int line_delta_mask = (1 << column_bits) - 1;
374 return offset + (((int)(token >> column_bits)) & line_delta_mask);
382 return (int) (token & column_mask);
390 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));
391 return checkpoints [CheckpointIndex].File;
395 public SourceFile SourceFile {
400 return source_list [index - 1];
404 #region IEquatable<Location> Members
406 public bool Equals (Location other)
408 return this.token == other.token;
415 // A bag of additional locations to support full ast tree
417 public class LocationsBag
419 public class MemberLocations
421 public readonly IList<Tuple<Modifiers, Location>> Modifiers;
422 List<Location> locations;
424 public MemberLocations (IList<Tuple<Modifiers, Location>> mods)
429 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location loc)
435 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
441 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, List<Location> locs)
449 public Location this [int index] {
451 return locations [index];
457 return locations.Count;
463 public void AddLocations (Location loc)
465 if (locations == null) {
466 locations = new List<Location> ();
472 public void AddLocations (params Location[] additional)
474 if (locations == null) {
475 locations = new List<Location> (additional);
477 locations.AddRange (additional);
482 Dictionary<object, List<Location>> simple_locs = new Dictionary<object, List<Location>> (ReferenceEquality<object>.Default);
483 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
485 [Conditional ("FULL_AST")]
486 public void AddLocation (object element, params Location[] locations)
488 simple_locs.Add (element, new List<Location> (locations));
491 [Conditional ("FULL_AST")]
492 public void InsertLocation (object element, int index, Location location)
494 List<Location> found;
495 if (!simple_locs.TryGetValue (element, out found)) {
496 found = new List<Location> ();
497 simple_locs.Add (element, found);
500 found.Insert (index, location);
503 [Conditional ("FULL_AST")]
504 public void AddStatement (object element, params Location[] locations)
506 if (locations.Length == 0)
507 throw new ArgumentException ("Statement is missing semicolon location");
509 AddLocation (element, locations);
512 [Conditional ("FULL_AST")]
513 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations)
515 member_locs.Add (member, new MemberLocations (modLocations));
518 [Conditional ("FULL_AST")]
519 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, Location location)
521 member_locs.Add (member, new MemberLocations (modLocations, location));
524 [Conditional ("FULL_AST")]
525 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
527 member_locs.Add (member, new MemberLocations (modLocations, locations));
530 [Conditional ("FULL_AST")]
531 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, List<Location> locations)
533 member_locs.Add (member, new MemberLocations (modLocations, locations));
536 [Conditional ("FULL_AST")]
537 public void AppendTo (object element, Location location)
539 List<Location> found;
540 if (!simple_locs.TryGetValue (element, out found)) {
541 found = new List<Location> ();
542 simple_locs.Add (element, found);
545 found.Add (location);
548 [Conditional ("FULL_AST")]
549 public void AppendToMember (MemberCore existing, params Location[] locations)
551 MemberLocations member;
552 if (member_locs.TryGetValue (existing, out member)) {
553 member.AddLocations (locations);
558 public List<Location> GetLocations (object element)
560 List<Location> found;
561 simple_locs.TryGetValue (element, out found);
565 public MemberLocations GetMemberLocation (MemberCore element)
567 MemberLocations found;
568 member_locs.TryGetValue (element, out found);