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;
21 public enum SourceFileType
28 // This is one single source file.
30 public class SourceFile : IEquatable<SourceFile>
33 // Used by #line directive to track hidden sequence point
36 struct LocationRegion : IComparable<LocationRegion>
38 public readonly Location Start;
39 public readonly Location End;
41 public LocationRegion (Location start, Location end)
47 public int CompareTo (LocationRegion other)
49 if (Start.Row == other.Start.Row)
50 return Start.Column.CompareTo (other.Start.Column);
52 return Start.Row.CompareTo (other.Start.Row);
55 public override string ToString ()
57 return Start.ToString () + " - " + End.ToString ();
61 static readonly byte[] MD5Algorith = { 96, 166, 110, 64, 207, 100, 130, 76, 182, 240, 66, 212, 129, 114, 167, 153 };
63 public readonly string Name;
64 public readonly string FullPathName;
65 public readonly int Index;
66 public bool AutoGenerated;
67 readonly SourceFileType FileType;
70 byte[] algGuid, checksum;
71 List<LocationRegion> hidden_lines;
73 public SourceFile (string name, string path, int index)
77 this.FullPathName = path;
79 if (name.EndsWith (".play", StringComparison.Ordinal) || name.EndsWith (".asx", StringComparison.Ordinal))
80 FileType = SourceFileType.PlayScript;
83 public byte[] Checksum {
89 public bool HasChecksum {
91 return checksum != null;
95 public bool IsPlayScript {
97 return FileType == SourceFileType.PlayScript;
101 public SourceFileEntry SourceFileEntry {
107 public void SetChecksum (byte[] checksum)
109 SetChecksum (MD5Algorith, checksum);
112 public void SetChecksum (byte[] algorithmGuid, byte[] checksum)
114 this.algGuid = algorithmGuid;
115 this.checksum = checksum;
118 public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter)
120 if (hidden_lines != null)
121 hidden_lines.Sort ();
123 file = new SourceFileEntry (symwriter, FullPathName, algGuid, checksum);
125 file.SetAutoGenerated ();
130 public bool Equals (SourceFile other)
132 return FullPathName == other.FullPathName;
135 public bool IsHiddenLocation (Location loc)
137 if (hidden_lines == null)
140 int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
143 var found = hidden_lines[index - 1];
144 if (loc.Row < found.End.Row)
151 public void RegisterHiddenScope (Location start, Location end)
153 if (hidden_lines == null)
154 hidden_lines = new List<LocationRegion> ();
156 hidden_lines.Add (new LocationRegion (start, end));
159 public override string ToString ()
161 return String.Format ("SourceFile ({0}:{1}:{2})", Name, FullPathName, Index);
166 /// Keeps track of the location in the program
170 /// This uses a compact representation and a couple of auxiliary
171 /// structures to keep track of tokens to (file,line and column)
172 /// mappings. The usage of the bits is:
174 /// - 16 bits for "checkpoint" which is a mixed concept of
175 /// file and "line segment"
176 /// - 8 bits for line delta (offset) from the line segment
177 /// - 8 bits for column number.
179 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
181 public struct Location : IEquatable<Location>
184 public readonly int LineOffset;
185 public readonly int File;
187 public Checkpoint (int file, int line)
190 LineOffset = line - (int) (line % (1 << line_delta_bits));
197 const int column_bits = 24;
198 const int line_delta_bits = 24;
202 const int column_bits = 8;
203 const int line_delta_bits = 8;
205 const int checkpoint_bits = 16;
207 const int column_mask = (1 << column_bits) - 1;
208 const int max_column = column_mask;
210 static List<SourceFile> source_list;
211 static Checkpoint [] checkpoints;
212 static int checkpoint_index;
214 public readonly static Location Null = new Location ();
215 public static bool InEmacs;
222 public static void Reset ()
224 source_list = new List<SourceFile> ();
225 checkpoint_index = 0;
228 public static void AddFile (SourceFile file)
230 source_list.Add (file);
234 // After adding all source files we want to compile with AddFile(), this method
235 // must be called to `reserve' an appropriate number of bits in the token for the
236 // source file. We reserve some extra space for files we encounter via #line
237 // directives while parsing.
239 static public void Initialize (List<SourceFile> files)
241 #if NET_4_0 || MONODROID
242 source_list.AddRange (files);
244 source_list.AddRange (files.ToArray ());
247 checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
248 if (checkpoints.Length > 0)
249 checkpoints [0] = new Checkpoint (0, 0);
252 public Location (SourceFile file, int row, int column)
257 if (column > max_column)
263 // TODO: For eval only, need better handling of empty
264 int file_index = file == null ? 0 : file.Index;
266 // FIXME: This value is certainly wrong but what was the intension
267 int max = checkpoint_index < 10 ?
268 checkpoint_index : 10;
269 for (int i = 0; i < max; i++) {
270 int offset = checkpoints [checkpoint_index - i].LineOffset;
271 delta = row - offset;
273 delta < (1 << line_delta_bits) &&
274 checkpoints[checkpoint_index - i].File == file_index) {
275 target = checkpoint_index - i;
280 AddCheckpoint (file_index, row);
281 target = checkpoint_index;
282 delta = row % (1 << line_delta_bits);
286 (delta << column_bits) +
287 (target << (line_delta_bits + column_bits));
291 token = l > 0xFFFFFFFF ? 0 : (int) l;
296 public static Location operator - (Location loc, int columns)
298 return new Location (loc.SourceFile, loc.Row, loc.Column - columns);
301 static void AddCheckpoint (int file, int row)
303 if (checkpoints.Length == ++checkpoint_index) {
304 Array.Resize (ref checkpoints, checkpoint_index * 2);
306 checkpoints [checkpoint_index] = new Checkpoint (file, row);
309 string FormatLocation (string fileName)
311 if (column_bits == 0 || InEmacs)
312 return fileName + "(" + Row.ToString () + "):";
314 return fileName + "(" + Row.ToString () + "," + Column.ToString () +
315 (Column == max_column ? "+):" : "):");
318 public override string ToString ()
320 return FormatLocation (Name);
323 public string ToStringFullName ()
325 return FormatLocation (NameFullPath);
329 /// Whether the Location is Null
332 get { return token == 0; }
338 if (token == 0 || index <= 0)
341 SourceFile file = source_list [index - 1];
346 public string NameFullPath {
349 if (token == 0 || index <= 0)
352 return source_list[index - 1].FullPathName;
356 int CheckpointIndex {
358 const int checkpoint_mask = (1 << checkpoint_bits) - 1;
359 return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
368 int offset = checkpoints[CheckpointIndex].LineOffset;
370 const int line_delta_mask = (1 << column_bits) - 1;
371 return offset + (((int)(token >> column_bits)) & line_delta_mask);
379 return (int) (token & column_mask);
387 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));
388 return checkpoints [CheckpointIndex].File;
392 // The ISymbolDocumentWriter interface is used by the symbol writer to
393 // describe a single source file - for each source file there's exactly
394 // one corresponding ISymbolDocumentWriter instance.
396 // This class has an internal hash table mapping source document names
397 // to such ISymbolDocumentWriter instances - so there's exactly one
398 // instance per document.
400 // This property returns the ISymbolDocumentWriter instance which belongs
401 // to the location's source file.
403 // If we don't have a symbol writer, this property is always null.
404 public SourceFile SourceFile {
409 return source_list [index - 1];
413 #region IEquatable<Location> Members
415 public bool Equals (Location other)
417 return this.token == other.token;
424 // A bag of additional locations to support full ast tree
426 public class LocationsBag
428 public class MemberLocations
430 public readonly IList<Tuple<Modifiers, Location>> Modifiers;
431 List<Location> locations;
433 public MemberLocations (IList<Tuple<Modifiers, Location>> mods)
438 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location loc)
444 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
450 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, List<Location> locs)
458 public Location this [int index] {
460 return locations [index];
466 return locations.Count;
472 public void AddLocations (Location loc)
474 if (locations == null) {
475 locations = new List<Location> ();
481 public void AddLocations (params Location[] additional)
483 if (locations == null) {
484 locations = new List<Location> (additional);
486 locations.AddRange (additional);
491 Dictionary<object, List<Location>> simple_locs = new Dictionary<object, List<Location>> (ReferenceEquality<object>.Default);
492 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
494 [Conditional ("FULL_AST")]
495 public void AddLocation (object element, params Location[] locations)
497 simple_locs.Add (element, new List<Location> (locations));
500 [Conditional ("FULL_AST")]
501 public void InsertLocation (object element, int index, Location location)
503 List<Location> found;
504 if (!simple_locs.TryGetValue (element, out found)) {
505 found = new List<Location> ();
506 simple_locs.Add (element, found);
509 found.Insert (index, location);
512 [Conditional ("FULL_AST")]
513 public void AddStatement (object element, params Location[] locations)
515 if (locations.Length == 0)
516 throw new ArgumentException ("Statement is missing semicolon location");
518 AddLocation (element, locations);
521 [Conditional ("FULL_AST")]
522 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations)
524 member_locs.Add (member, new MemberLocations (modLocations));
527 [Conditional ("FULL_AST")]
528 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, Location location)
530 member_locs.Add (member, new MemberLocations (modLocations, location));
533 [Conditional ("FULL_AST")]
534 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
536 member_locs.Add (member, new MemberLocations (modLocations, locations));
539 [Conditional ("FULL_AST")]
540 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, List<Location> locations)
542 member_locs.Add (member, new MemberLocations (modLocations, locations));
545 [Conditional ("FULL_AST")]
546 public void AppendTo (object element, Location location)
548 List<Location> found;
549 if (!simple_locs.TryGetValue (element, out found)) {
550 found = new List<Location> ();
551 simple_locs.Add (element, found);
554 found.Add (location);
557 [Conditional ("FULL_AST")]
558 public void AppendToMember (MemberCore existing, params Location[] locations)
560 MemberLocations member;
561 if (member_locs.TryGetValue (existing, out member)) {
562 member.AddLocations (locations);
567 public List<Location> GetLocations (object element)
569 List<Location> found;
570 simple_locs.TryGetValue (element, out found);
574 public MemberLocations GetMemberLocation (MemberCore element)
576 MemberLocations found;
577 member_locs.TryGetValue (element, out found);