[mcs] Add pathmap option
[mono.git] / mcs / mcs / location.cs
1 //
2 // location.cs: Keeps track of the location of source code entity
3 //
4 // Author:
5 //   Miguel de Icaza
6 //   Atsushi Enomoto  <atsushi@ximian.com>
7 //   Marek Safar (marek.safar@gmail.com)
8 //
9 // Copyright 2001 Ximian, Inc.
10 // Copyright 2005 Novell, Inc.
11 //
12
13 using System;
14 using System.Collections.Generic;
15 using Mono.CompilerServices.SymbolWriter;
16 using System.Diagnostics;
17 using System.Linq;
18 using System.IO;
19
20 namespace Mono.CSharp
21 {
22         //
23         //  This is one single source file.
24         //
25         public class SourceFile : IEquatable<SourceFile>
26         {
27                 //
28                 // Used by #line directive to track hidden sequence point
29                 // regions
30                 //
31                 struct LocationRegion : IComparable<LocationRegion>
32                 {
33                         public readonly Location Start;
34                         public readonly Location End;
35
36                         public LocationRegion (Location start, Location end)
37                         {
38                                 this.Start = start;
39                                 this.End = end;
40                         }
41
42                         public int CompareTo (LocationRegion other)
43                         {
44                                 if (Start.Row == other.Start.Row)
45                                         return Start.Column.CompareTo (other.Start.Column);
46
47                                 return Start.Row.CompareTo (other.Start.Row);
48                         }
49
50                         public override string ToString ()
51                         {
52                                 return Start.ToString () + " - " + End.ToString ();
53                         }
54                 }
55
56                 static readonly byte[] MD5Algorith = { 96, 166, 110, 64, 207, 100, 130, 76, 182, 240, 66, 212, 129, 114, 167, 153 };
57
58                 public readonly string Name;
59                 public readonly string OriginalFullPathName;
60                 public readonly int Index;
61                 public bool AutoGenerated;
62
63                 SourceFileEntry file;
64                 byte[] algGuid, checksum;
65                 List<LocationRegion> hidden_lines;
66
67                 public SourceFile (string name, string path, int index)
68                 {
69                         this.Index = index;
70                         this.Name = name;
71                         this.OriginalFullPathName = path;
72                 }
73
74                 public byte[] Checksum {
75                         get {
76                                 return checksum;
77                         }
78                 }
79
80                 public bool HasChecksum {
81                         get {
82                                 return checksum != null;
83                         }
84                 }
85
86                 public SourceFileEntry SourceFileEntry {
87                         get {
88                                 return file;
89                         }
90                 }
91
92                 public void SetChecksum (byte[] checksum)
93                 {
94                         SetChecksum (MD5Algorith, checksum);
95                 }
96
97                 public void SetChecksum (byte[] algorithmGuid, byte[] checksum)
98                 {
99                         this.algGuid = algorithmGuid;
100                         this.checksum = checksum;
101                 }
102
103                 public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter, List<KeyValuePair<string, string>> pathMap)
104                 {
105                         if (hidden_lines != null)
106                                 hidden_lines.Sort ();
107
108                         file = new SourceFileEntry (symwriter, GetFullPathName (pathMap), OriginalFullPathName, algGuid, checksum);
109                         if (AutoGenerated)
110                                 file.SetAutoGenerated ();
111
112                         return file;
113                 }
114
115                 public string GetFullPathName (List<KeyValuePair<string, string>> pathMap)
116                 {
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)
122                                                 continue;
123
124                                         if (path [prefix.Length] != Path.DirectorySeparatorChar)
125                                                 continue;
126
127                                         if (!path.StartsWith (prefix, StringComparison.Ordinal))
128                                                 continue;
129
130                                         path = map.Value + path.Substring (prefix.Length);
131                                 }
132                         }
133
134                         return path;
135                 }
136
137                 public bool Equals (SourceFile other)
138                 {
139                         return OriginalFullPathName == other.OriginalFullPathName;
140                 }
141
142                 public bool IsHiddenLocation (Location loc)
143                 {
144                         if (hidden_lines == null)
145                                 return false;
146
147                         int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
148                         index = ~index;
149                         if (index > 0) {
150                                 var found = hidden_lines[index - 1];
151                                 if (loc.Row < found.End.Row)
152                                         return true;
153                         }
154
155                         return false;
156                 }
157
158                 public void RegisterHiddenScope (Location start, Location end)
159                 {
160                         if (hidden_lines == null)
161                                 hidden_lines = new List<LocationRegion> ();
162
163                         hidden_lines.Add (new LocationRegion (start, end));
164                 }
165
166                 public override string ToString ()
167                 {
168                         return String.Format ("SourceFile ({0}:{1}:{2})", Name, OriginalFullPathName, Index);
169                 }
170         }
171
172         /// <summary>
173         ///   Keeps track of the location in the program
174         /// </summary>
175         ///
176         /// <remarks>
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:
180         ///   
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.
185         ///
186         ///   http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
187         /// </remarks>
188         public struct Location : IEquatable<Location>
189         {
190                 struct Checkpoint {
191                         public readonly int LineOffset;
192                         public readonly int File;
193
194                         public Checkpoint (int file, int line)
195                         {
196                                 File = file;
197                                 LineOffset = line - (int) (line % (1 << line_delta_bits));
198                         }
199                 }
200
201 #if FULL_AST
202                 readonly long token;
203
204                 const int column_bits = 24;
205                 const int line_delta_bits = 24;
206 #else
207                 readonly int token;
208
209                 const int column_bits = 8;
210                 const int line_delta_bits = 8;
211 #endif
212                 const int checkpoint_bits = 16;
213
214                 const int column_mask = (1 << column_bits) - 1;
215                 const int max_column = column_mask;
216
217                 static List<SourceFile> source_list;
218                 static Checkpoint [] checkpoints;
219                 static int checkpoint_index;
220                 
221                 public readonly static Location Null = new Location ();
222                 public static bool InEmacs;
223                 
224                 static Location ()
225                 {
226                         Reset ();
227                 }
228
229                 public static void Reset ()
230                 {
231                         source_list = new List<SourceFile> ();
232                         checkpoint_index = 0;
233                 }
234
235                 public static void AddFile (SourceFile file)
236                 {
237                         source_list.Add (file);
238                 }
239
240                 // <summary>
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.
245                 // </summary>
246                 static public void Initialize (List<SourceFile> files)
247                 {
248                         source_list.AddRange (files);
249
250                         checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
251                         if (checkpoints.Length > 0)
252                                 checkpoints [0] = new Checkpoint (0, 0);
253                 }
254
255                 public Location (SourceFile file, int row, int column)
256                 {
257                         if (row <= 0)
258                                 token = 0;
259                         else {
260                                 if (column > max_column)
261                                         column = max_column;
262
263                                 long target = -1;
264                                 long delta = 0;
265
266                                 // TODO: For eval only, need better handling of empty
267                                 int file_index = file == null ? 0 : file.Index;
268
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;
275                                         if (delta >= 0 &&
276                                                 delta < (1 << line_delta_bits) &&
277                                                 checkpoints[checkpoint_index - i].File == file_index) {
278                                                 target = checkpoint_index - i;
279                                                 break;
280                                         }
281                                 }
282                                 if (target == -1) {
283                                         AddCheckpoint (file_index, row);
284                                         target = checkpoint_index;
285                                         delta = row % (1 << line_delta_bits);
286                                 }
287
288                                 long l = column +
289                                         (delta << column_bits) +
290                                         (target << (line_delta_bits + column_bits));
291 #if FULL_AST
292                                 token = l;
293 #else
294                                 token = l > 0xFFFFFFFF ? 0 : (int) l;
295 #endif
296                         }
297                 }
298
299                 public static Location operator - (Location loc, int columns)
300                 {
301                         return new Location (loc.SourceFile, loc.Row, loc.Column - columns);
302                 }
303
304                 static void AddCheckpoint (int file, int row)
305                 {
306                         if (checkpoints.Length == ++checkpoint_index) {
307                                 Array.Resize (ref checkpoints, checkpoint_index * 2);
308                         }
309                         checkpoints [checkpoint_index] = new Checkpoint (file, row);
310                 }
311
312                 string FormatLocation (string fileName)
313                 {
314                         if (column_bits == 0 || InEmacs)
315                                 return fileName + "(" + Row.ToString () + "):";
316
317                         return fileName + "(" + Row.ToString () + "," + Column.ToString () +
318                                 (Column == max_column ? "+):" : "):");
319                 }
320                 
321                 public override string ToString ()
322                 {
323                         return FormatLocation (Name);
324                 }
325
326                 public string ToStringFullName ()
327                 {
328                         return FormatLocation (NameFullPath);
329                 }
330                 
331                 /// <summary>
332                 ///   Whether the Location is Null
333                 /// </summary>
334                 public bool IsNull {
335                         get { return token == 0; }
336                 }
337
338                 public string Name {
339                         get {
340                                 int index = File;
341                                 if (token == 0 || index <= 0)
342                                         return null;
343
344                                 SourceFile file = source_list [index - 1];
345                                 return file.Name;
346                         }
347                 }
348
349                 public string NameFullPath {
350                         get {
351                                 int index = File;
352                                 if (index <= 0)
353                                         return null;
354
355                                 return source_list[index - 1].OriginalFullPathName;
356                         }
357                 }
358
359                 int CheckpointIndex {
360                         get {
361                                 const int checkpoint_mask = (1 << checkpoint_bits) - 1;
362                                 return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
363                         }
364                 }
365
366                 public int Row {
367                         get {
368                                 if (token == 0)
369                                         return 1;
370
371                                 int offset = checkpoints[CheckpointIndex].LineOffset;
372
373                                 const int line_delta_mask = (1 << column_bits) - 1;
374                                 return offset + (((int)(token >> column_bits)) & line_delta_mask);
375                         }
376                 }
377
378                 public int Column {
379                         get {
380                                 if (token == 0)
381                                         return 1;
382                                 return (int) (token & column_mask);
383                         }
384                 }
385
386                 public int File {
387                         get {
388                                 if (token == 0)
389                                         return 0;
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;
392                         }
393                 }
394
395                 public SourceFile SourceFile {
396                         get {
397                                 int index = File;
398                                 if (index == 0)
399                                         return null;
400                                 return source_list [index - 1];
401                         }
402                 }
403
404                 #region IEquatable<Location> Members
405
406                 public bool Equals (Location other)
407                 {
408                         return this.token == other.token;
409                 }
410
411                 #endregion
412         }
413
414         //
415         // A bag of additional locations to support full ast tree
416         //
417         public class LocationsBag
418         {
419                 public class MemberLocations
420                 {
421                         public readonly IList<Tuple<Modifiers, Location>> Modifiers;
422                         List<Location> locations;
423
424                         public MemberLocations (IList<Tuple<Modifiers, Location>> mods)
425                         {
426                                 Modifiers = mods;
427                         }
428
429                         public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location loc)
430                                 : this (mods)
431                         {
432                                 AddLocations (loc);
433                         }
434
435                         public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
436                                 : this (mods)
437                         {
438                                 AddLocations (locs);
439                         }
440
441                         public MemberLocations (IList<Tuple<Modifiers, Location>> mods, List<Location> locs)
442                                 : this (mods)
443                         {
444                                 locations = locs;
445                         }
446
447                         #region Properties
448
449                         public Location this [int index] {
450                                 get {
451                                         return locations [index];
452                                 }
453                         }
454                         
455                         public int Count {
456                                 get {
457                                         return locations.Count;
458                                 }
459                         }
460
461                         #endregion
462
463                         public void AddLocations (Location loc)
464                         {
465                                 if (locations == null) {
466                                         locations = new List<Location> ();
467                                 }
468
469                                 locations.Add (loc);
470                         }
471
472                         public void AddLocations (params Location[] additional)
473                         {
474                                 if (locations == null) {
475                                         locations = new List<Location> (additional);
476                                 } else {
477                                         locations.AddRange (additional);
478                                 }
479                         }
480                 }
481
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);
484
485                 [Conditional ("FULL_AST")]
486                 public void AddLocation (object element, params Location[] locations)
487                 {
488                         simple_locs.Add (element, new List<Location> (locations));
489                 }
490
491                 [Conditional ("FULL_AST")]
492                 public void InsertLocation (object element, int index, Location location)
493                 {
494                         List<Location> found;
495                         if (!simple_locs.TryGetValue (element, out found)) {
496                                 found = new List<Location> ();
497                                 simple_locs.Add (element, found);
498                         }
499
500                         found.Insert (index, location);
501                 }
502
503                 [Conditional ("FULL_AST")]
504                 public void AddStatement (object element, params Location[] locations)
505                 {
506                         if (locations.Length == 0)
507                                 throw new ArgumentException ("Statement is missing semicolon location");
508
509                         AddLocation (element, locations);
510                 }
511
512                 [Conditional ("FULL_AST")]
513                 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations)
514                 {
515                         member_locs.Add (member, new MemberLocations (modLocations));
516                 }
517
518                 [Conditional ("FULL_AST")]
519                 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, Location location)
520                 {
521                         member_locs.Add (member, new MemberLocations (modLocations, location));
522                 }
523
524                 [Conditional ("FULL_AST")]
525                 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
526                 {
527                         member_locs.Add (member, new MemberLocations (modLocations, locations));
528                 }
529
530                 [Conditional ("FULL_AST")]
531                 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, List<Location> locations)
532                 {
533                         member_locs.Add (member, new MemberLocations (modLocations, locations));
534                 }
535
536                 [Conditional ("FULL_AST")]
537                 public void AppendTo (object element, Location location)
538                 {
539                         List<Location> found;
540                         if (!simple_locs.TryGetValue (element, out found)) {
541                                 found = new List<Location> ();
542                                 simple_locs.Add (element, found);
543                         }
544
545                         found.Add (location);
546                 }
547
548                 [Conditional ("FULL_AST")]
549                 public void AppendToMember (MemberCore existing, params Location[] locations)
550                 {
551                         MemberLocations member;
552                         if (member_locs.TryGetValue (existing, out member)) {
553                                 member.AddLocations (locations);
554                                 return;
555                         }
556                 }
557
558                 public List<Location> GetLocations (object element)
559                 {
560                         List<Location> found;
561                         simple_locs.TryGetValue (element, out found);
562                         return found;
563                 }
564
565                 public MemberLocations GetMemberLocation (MemberCore element)
566                 {
567                         MemberLocations found;
568                         member_locs.TryGetValue (element, out found);
569                         return found;
570                 }
571         }
572 }