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 public readonly string Name;
56 public readonly string FullPathName;
57 public readonly int Index;
58 public bool AutoGenerated;
61 byte[] guid, checksum;
62 List<LocationRegion> hidden_lines;
64 public SourceFile (string name, string path, int index)
68 this.FullPathName = path;
71 public SourceFileEntry SourceFileEntry {
77 public void SetChecksum (byte[] guid, byte[] checksum)
80 this.checksum = checksum;
83 public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter)
85 if (hidden_lines != null)
89 file = new SourceFileEntry (symwriter, FullPathName, guid, checksum);
91 file = new SourceFileEntry (symwriter, FullPathName);
93 file.SetAutoGenerated ();
99 public bool Equals (SourceFile other)
101 return FullPathName == other.FullPathName;
104 public bool IsHiddenLocation (Location loc)
106 if (hidden_lines == null)
109 int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
112 var found = hidden_lines[index - 1];
113 if (loc.Row < found.End.Row)
120 public void RegisterHiddenScope (Location start, Location end)
122 if (hidden_lines == null)
123 hidden_lines = new List<LocationRegion> ();
125 hidden_lines.Add (new LocationRegion (start, end));
128 public override string ToString ()
130 return String.Format ("SourceFile ({0}:{1}:{2})", Name, FullPathName, Index);
135 /// Keeps track of the location in the program
139 /// This uses a compact representation and a couple of auxiliary
140 /// structures to keep track of tokens to (file,line and column)
141 /// mappings. The usage of the bits is:
143 /// - 16 bits for "checkpoint" which is a mixed concept of
144 /// file and "line segment"
145 /// - 8 bits for line delta (offset) from the line segment
146 /// - 8 bits for column number.
148 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
150 public struct Location : IEquatable<Location>
153 public readonly int LineOffset;
154 public readonly int File;
156 public Checkpoint (int file, int line)
159 LineOffset = line - (int) (line % (1 << line_delta_bits));
166 const int column_bits = 24;
167 const int line_delta_bits = 24;
171 const int column_bits = 8;
172 const int line_delta_bits = 8;
174 const int checkpoint_bits = 16;
176 const int column_mask = (1 << column_bits) - 1;
177 const int max_column = column_mask;
179 static List<SourceFile> source_list;
180 static int current_source;
181 static Checkpoint [] checkpoints;
182 static int checkpoint_index;
184 public readonly static Location Null = new Location ();
185 public static bool InEmacs;
192 public static void Reset ()
194 source_list = new List<SourceFile> ();
196 checkpoint_index = 0;
199 public static void AddFile (SourceFile file)
201 source_list.Add (file);
205 // After adding all source files we want to compile with AddFile(), this method
206 // must be called to `reserve' an appropriate number of bits in the token for the
207 // source file. We reserve some extra space for files we encounter via #line
208 // directives while parsing.
210 static public void Initialize (List<SourceFile> files)
212 #if NET_4_0 || MONODROID
213 source_list.AddRange (files);
215 source_list.AddRange (files.ToArray ());
218 checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
219 if (checkpoints.Length > 0)
220 checkpoints [0] = new Checkpoint (0, 0);
223 static public void Push (SourceFile file)
225 current_source = file != null ? file.Index : -1;
226 // File is always pushed before being changed.
229 public Location (int row, int column)
234 if (column > max_column)
240 // FIXME: This value is certainly wrong but what was the intension
241 int max = checkpoint_index < 10 ?
242 checkpoint_index : 10;
243 for (int i = 0; i < max; i++) {
244 int offset = checkpoints [checkpoint_index - i].LineOffset;
245 delta = row - offset;
247 delta < (1 << line_delta_bits) &&
248 checkpoints [checkpoint_index - i].File == current_source) {
249 target = checkpoint_index - i;
254 AddCheckpoint (current_source, row);
255 target = checkpoint_index;
256 delta = row % (1 << line_delta_bits);
260 (delta << column_bits) +
261 (target << (line_delta_bits + column_bits));
265 token = l > 0xFFFFFFFF ? 0 : (int) l;
270 public static Location operator - (Location loc, int columns)
272 return new Location (loc.Row, loc.Column - columns);
275 static void AddCheckpoint (int file, int row)
277 if (checkpoints.Length == ++checkpoint_index) {
278 Array.Resize (ref checkpoints, checkpoint_index * 2);
280 checkpoints [checkpoint_index] = new Checkpoint (file, row);
283 string FormatLocation (string fileName)
285 if (column_bits == 0 || InEmacs)
286 return fileName + "(" + Row.ToString () + "):";
288 return fileName + "(" + Row.ToString () + "," + Column.ToString () +
289 (Column == max_column ? "+):" : "):");
292 public override string ToString ()
294 return FormatLocation (Name);
297 public string ToStringFullName ()
299 return FormatLocation (NameFullPath);
303 /// Whether the Location is Null
306 get { return token == 0; }
312 if (token == 0 || index <= 0)
315 SourceFile file = source_list [index - 1];
320 public string NameFullPath {
323 if (token == 0 || index <= 0)
326 return source_list[index - 1].FullPathName;
330 int CheckpointIndex {
332 const int checkpoint_mask = (1 << checkpoint_bits) - 1;
333 return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
342 int offset = checkpoints[CheckpointIndex].LineOffset;
344 const int line_delta_mask = (1 << column_bits) - 1;
345 return offset + (((int)(token >> column_bits)) & line_delta_mask);
353 return (int) (token & column_mask);
361 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));
362 return checkpoints [CheckpointIndex].File;
366 // The ISymbolDocumentWriter interface is used by the symbol writer to
367 // describe a single source file - for each source file there's exactly
368 // one corresponding ISymbolDocumentWriter instance.
370 // This class has an internal hash table mapping source document names
371 // to such ISymbolDocumentWriter instances - so there's exactly one
372 // instance per document.
374 // This property returns the ISymbolDocumentWriter instance which belongs
375 // to the location's source file.
377 // If we don't have a symbol writer, this property is always null.
378 public SourceFile SourceFile {
383 return source_list [index - 1];
387 #region IEquatable<Location> Members
389 public bool Equals (Location other)
391 return this.token == other.token;
398 // A bag of additional locations to support full ast tree
400 public class LocationsBag
402 public class MemberLocations
404 public readonly IList<Tuple<Modifiers, Location>> Modifiers;
405 Location[] locations;
407 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
415 public Location this [int index] {
417 return locations [index];
423 return locations.Length;
429 public void AddLocations (params Location[] additional)
431 if (locations == null) {
432 locations = additional;
434 int pos = locations.Length;
435 Array.Resize (ref locations, pos + additional.Length);
436 additional.CopyTo (locations, pos);
441 Dictionary<object, Location[]> simple_locs = new Dictionary<object, Location[]> (ReferenceEquality<object>.Default);
442 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
444 [Conditional ("FULL_AST")]
445 public void AddLocation (object element, params Location[] locations)
447 simple_locs.Add (element, locations);
450 [Conditional ("FULL_AST")]
451 public void AddStatement (object element, params Location[] locations)
453 if (locations.Length == 0)
454 throw new ArgumentException ("Statement is missing semicolon location");
456 simple_locs.Add (element, locations);
459 [Conditional ("FULL_AST")]
460 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
462 member_locs.Add (member, new MemberLocations (modLocations, locations));
465 [Conditional ("FULL_AST")]
466 public void AppendTo (object existing, params Location[] locations)
469 if (simple_locs.TryGetValue (existing, out locs)) {
470 simple_locs [existing] = locs.Concat (locations).ToArray ();
475 [Conditional ("FULL_AST")]
476 public void AppendToMember (MemberCore existing, params Location[] locations)
478 MemberLocations member;
479 if (member_locs.TryGetValue (existing, out member)) {
480 member.AddLocations (locations);
485 public Location[] GetLocations (object element)
488 simple_locs.TryGetValue (element, out found);
492 public MemberLocations GetMemberLocation (MemberCore element)
494 MemberLocations found;
495 member_locs.TryGetValue (element, out found);