[666376] Implement compiler option fullpaths
[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.IO;
15 using System.Collections.Generic;
16 using Mono.CompilerServices.SymbolWriter;
17 using System.Diagnostics;
18 using System.Linq;
19
20 namespace Mono.CSharp {
21         /// <summary>
22         ///   This is one single source file.
23         /// </summary>
24         /// <remarks>
25         ///   This is intentionally a class and not a struct since we need
26         ///   to pass this by reference.
27         /// </remarks>
28         public class SourceFile : ISourceFile {
29                 public readonly string Name;
30                 public readonly string Path;
31                 public readonly int Index;
32                 public bool AutoGenerated;
33                 public bool IsIncludeFile;
34
35                 SourceFileEntry file;
36                 byte[] guid, checksum;
37
38                 public SourceFile (string name, string path, int index, bool is_include)
39                 {
40                         this.Index = index;
41                         this.Name = name;
42                         this.Path = path;
43                         this.IsIncludeFile = is_include;
44                 }
45
46                 public SourceFileEntry SourceFileEntry {
47                         get { return file; }
48                 }
49
50                 SourceFileEntry ISourceFile.Entry {
51                         get { return file; }
52                 }
53
54                 public void SetChecksum (byte[] guid, byte[] checksum)
55                 {
56                         this.guid = guid;
57                         this.checksum = checksum;
58                 }
59
60                 public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter)
61                 {
62                         if (guid != null)
63                                 file = symwriter.DefineDocument (Path, guid, checksum);
64                         else {
65                                 file = symwriter.DefineDocument (Path);
66                                 if (AutoGenerated)
67                                         file.SetAutoGenerated ();
68                         }
69                 }
70
71                 public override string ToString ()
72                 {
73                         return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
74                                               Name, Path, Index, SourceFileEntry);
75                 }
76         }
77
78         public class CompilationUnit : SourceFile, ICompileUnit
79         {
80                 CompileUnitEntry comp_unit;
81                 Dictionary<string, SourceFile> include_files;
82                 Dictionary<string, bool> conditionals;
83
84                 public CompilationUnit (string name, string path, int index)
85                         : base (name, path, index, false)
86                 { }
87
88                 public void AddFile (SourceFile file)
89                 {
90                         if (file == this)
91                                 return;
92                         
93                         if (include_files == null)
94                                 include_files = new Dictionary<string, SourceFile> ();
95
96                         if (!include_files.ContainsKey (file.Path))
97                                 include_files.Add (file.Path, file);
98                 }
99
100                 public void AddDefine (string value)
101                 {
102                         if (conditionals == null)
103                                 conditionals = new Dictionary<string, bool> (2);
104
105                         conditionals [value] = true;
106                 }
107
108                 public void AddUndefine (string value)
109                 {
110                         if (conditionals == null)
111                                 conditionals = new Dictionary<string, bool> (2);
112
113                         conditionals [value] = false;
114                 }
115
116                 CompileUnitEntry ICompileUnit.Entry {
117                         get { return comp_unit; }
118                 }
119
120                 public CompileUnitEntry CompileUnitEntry {
121                         get { return comp_unit; }
122                 }
123
124                 public override void DefineSymbolInfo (MonoSymbolWriter symwriter)
125                 {
126                         base.DefineSymbolInfo (symwriter);
127
128                         comp_unit = symwriter.DefineCompilationUnit (SourceFileEntry);
129
130                         if (include_files != null) {
131                                 foreach (SourceFile include in include_files.Values) {
132                                         include.DefineSymbolInfo (symwriter);
133                                         comp_unit.AddFile (include.SourceFileEntry);
134                                 }
135                         }
136                 }
137
138                 public bool IsConditionalDefined (string value)
139                 {
140                         if (conditionals != null) {
141                                 bool res;
142                                 if (conditionals.TryGetValue (value, out res))
143                                         return res;
144                                 
145                                 // When conditional was undefined
146                                 if (conditionals.ContainsKey (value))
147                                         return false;                                   
148                         }
149
150                         return RootContext.IsConditionalDefined (value);
151                 }
152         }
153
154         /// <summary>
155         ///   Keeps track of the location in the program
156         /// </summary>
157         ///
158         /// <remarks>
159         ///   This uses a compact representation and a couple of auxiliary
160         ///   structures to keep track of tokens to (file,line and column) 
161         ///   mappings. The usage of the bits is:
162         ///   
163         ///     - 16 bits for "checkpoint" which is a mixed concept of
164         ///       file and "line segment"
165         ///     - 8 bits for line delta (offset) from the line segment
166         ///     - 8 bits for column number.
167         ///
168         ///   http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
169         /// </remarks>
170         public struct Location : IEquatable<Location>
171         {
172                 int token; 
173
174                 struct Checkpoint {
175                         public readonly int LineOffset;
176                         public readonly int CompilationUnit;
177                         public readonly int File;
178
179                         public Checkpoint (int compile_unit, int file, int line)
180                         {
181                                 File = file;
182                                 CompilationUnit = compile_unit;
183                                 LineOffset = line - (int) (line % (1 << line_delta_bits));
184                         }
185                 }
186
187                 static List<SourceFile> source_list;
188                 static List<CompilationUnit> compile_units;
189                 static Dictionary<string, int> source_files;
190                 static int checkpoint_bits;
191                 static int source_count;
192                 static int current_source;
193                 static int current_compile_unit;
194                 static int line_delta_bits;
195                 static int line_delta_mask;
196                 static int column_bits;
197                 static int column_mask;
198                 static Checkpoint [] checkpoints;
199                 static int checkpoint_index;
200                 
201                 public readonly static Location Null = new Location (-1);
202                 public static bool InEmacs;
203                 
204                 static Location ()
205                 {
206                         Reset ();
207                         checkpoints = new Checkpoint [10];
208                 }
209
210                 public static void Reset ()
211                 {
212                         source_files = new Dictionary<string, int> ();
213                         source_list = new List<SourceFile> ();
214                         compile_units = new List<CompilationUnit> ();
215                         current_source = 0;
216                         current_compile_unit = 0;
217                         source_count = 0;
218                 }
219
220                 // <summary>
221                 //   This must be called before parsing/tokenizing any files.
222                 // </summary>
223                 static public void AddFile (Report r, string name)
224                 {
225                         string path = Path.GetFullPath (name);
226                         int id;
227                         if (source_files.TryGetValue (path, out id)){
228                                 string other_name = source_list [id - 1].Name;
229                                 if (name.Equals (other_name))
230                                         r.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
231                                 else
232                                         r.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
233                                 return;
234                         }
235
236                         source_files.Add (path, ++source_count);
237                         CompilationUnit unit = new CompilationUnit (name, path, source_count);
238                         source_list.Add (unit);
239                         compile_units.Add (unit);
240                 }
241
242                 public static IList<CompilationUnit> SourceFiles {
243                         get {
244                                 return compile_units;
245                         }
246                 }
247
248                 // <summary>
249                 //   After adding all source files we want to compile with AddFile(), this method
250                 //   must be called to `reserve' an appropriate number of bits in the token for the
251                 //   source file.  We reserve some extra space for files we encounter via #line
252                 //   directives while parsing.
253                 // </summary>
254                 static public void Initialize ()
255                 {
256                         checkpoints = new Checkpoint [source_list.Count * 2];
257                         if (checkpoints.Length > 0)
258                                 checkpoints [0] = new Checkpoint (0, 0, 0);
259
260                         column_bits = 8;
261                         column_mask = 0xFF;
262                         line_delta_bits = 8;
263                         line_delta_mask = 0xFF00;
264                         checkpoint_index = 0;
265                         checkpoint_bits = 16;
266                 }
267
268                 // <remarks>
269                 //   This is used when we encounter a #line preprocessing directive.
270                 // </remarks>
271                 static public SourceFile LookupFile (CompilationUnit comp_unit, string name)
272                 {
273                         string path;
274                         if (!Path.IsPathRooted (name)) {
275                                 string root = Path.GetDirectoryName (comp_unit.Path);
276                                 path = Path.Combine (root, name);
277                         } else
278                                 path = name;
279
280                         if (!source_files.ContainsKey (path)) {
281                                 if (source_count >= (1 << checkpoint_bits))
282                                         return new SourceFile (name, path, 0, true);
283
284                                 source_files.Add (path, ++source_count);
285                                 SourceFile retval = new SourceFile (name, path, source_count, true);
286                                 source_list.Add (retval);
287                                 return retval;
288                         }
289
290                         int index = (int) source_files [path];
291                         return source_list [index - 1];
292                 }
293
294                 static public void Push (CompilationUnit compile_unit, SourceFile file)
295                 {
296                         current_source = file != null ? file.Index : -1;
297                         current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
298                         // File is always pushed before being changed.
299                 }
300
301                 // <remarks>
302                 //   If we're compiling with debugging support, this is called between parsing
303                 //   and code generation to register all the source files with the
304                 //   symbol writer.
305                 // </remarks>
306                 static public void DefineSymbolDocuments (MonoSymbolWriter symwriter)
307                 {
308                         foreach (CompilationUnit unit in compile_units)
309                                 unit.DefineSymbolInfo (symwriter);
310                 }
311                 
312                 public Location (int row)
313                         : this (row, 0)
314                 {
315                 }
316
317                 public Location (int row, int column)
318                 {
319                         if (row <= 0)
320                                 token = 0;
321                         else {
322                                 if (column > 254)
323                                         column = 254;
324                                 if (column < 0)
325                                         column = 255;
326                                 int target = -1;
327                                 int delta = 0;
328                                 int max = checkpoint_index < 10 ?
329                                         checkpoint_index : 10;
330                                 for (int i = 0; i < max; i++) {
331                                         int offset = checkpoints [checkpoint_index - i].LineOffset;
332                                         delta = row - offset;
333                                         if (delta >= 0 &&
334                                                 delta < (1 << line_delta_bits) &&
335                                                 checkpoints [checkpoint_index - i].File == current_source) {
336                                                 target = checkpoint_index - i;
337                                                 break;
338                                         }
339                                 }
340                                 if (target == -1) {
341                                         AddCheckpoint (current_compile_unit, current_source, row);
342                                         target = checkpoint_index;
343                                         delta = row % (1 << line_delta_bits);
344                                 }
345                                 long l = column +
346                                         (long) (delta << column_bits) +
347                                         (long) (target << (line_delta_bits + column_bits));
348                                 token = l > 0xFFFFFFFF ? 0 : (int) l;
349                         }
350                 }
351
352                 public static Location operator - (Location loc, int columns)
353                 {
354                         return new Location (loc.Row, loc.Column - columns);
355                 }
356
357                 static void AddCheckpoint (int compile_unit, int file, int row)
358                 {
359                         if (checkpoints.Length == ++checkpoint_index) {
360                                 Checkpoint [] tmp = new Checkpoint [checkpoint_index * 2];
361                                 Array.Copy (checkpoints, tmp, checkpoints.Length);
362                                 checkpoints = tmp;
363                         }
364                         checkpoints [checkpoint_index] = new Checkpoint (compile_unit, file, row);
365                 }
366
367                 string FormatLocation (string fileName)
368                 {
369                         if (column_bits == 0 || InEmacs)
370                                 return fileName + "(" + Row.ToString () + "):";
371
372                         return fileName + "(" + Row.ToString () + "," + Column.ToString () +
373                                 (Column == column_mask ? "+):" : "):");
374                 }
375                 
376                 public override string ToString ()
377                 {
378                         return FormatLocation (Name);
379                 }
380
381                 public string ToStringFullName ()
382                 {
383                         return FormatLocation (NameFullPath);
384                 }
385                 
386                 /// <summary>
387                 ///   Whether the Location is Null
388                 /// </summary>
389                 public bool IsNull {
390                         get { return token == 0; }
391                 }
392
393                 public string Name {
394                         get {
395                                 int index = File;
396                                 if (token == 0 || index == 0)
397                                         return "Internal";
398
399                                 SourceFile file = source_list [index - 1];
400                                 return file.Name;
401                         }
402                 }
403
404                 public string NameFullPath {
405                         get {
406                                 int index = File;
407                                 if (token == 0 || index == 0)
408                                         return "Internal";
409
410                                 return source_list [index - 1].Path;
411                         }
412                 }
413
414                 int CheckpointIndex {
415                         get { return (int) ((token & 0xFFFF0000) >> (line_delta_bits + column_bits)); }
416                 }
417
418                 public int Row {
419                         get {
420                                 if (token == 0)
421                                         return 1;
422                                 return checkpoints [CheckpointIndex].LineOffset + ((token & line_delta_mask) >> column_bits);
423                         }
424                 }
425
426                 public int Column {
427                         get {
428                                 if (token == 0)
429                                         return 1;
430                                 int col = (int) (token & column_mask);
431                                 return col == 255 ? 1 : col;
432                         }
433                 }
434
435                 public bool Hidden {
436                         get {
437                                 return (int) (token & column_mask) == 255;
438                         }
439                 }
440
441                 public int CompilationUnitIndex {
442                         get {
443                                 if (token == 0)
444                                         return 0;
445 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));
446                                 return checkpoints [CheckpointIndex].CompilationUnit;
447                         }
448                 }
449
450                 public int File {
451                         get {
452                                 if (token == 0)
453                                         return 0;
454 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));
455                                 return checkpoints [CheckpointIndex].File;
456                         }
457                 }
458
459                 // The ISymbolDocumentWriter interface is used by the symbol writer to
460                 // describe a single source file - for each source file there's exactly
461                 // one corresponding ISymbolDocumentWriter instance.
462                 //
463                 // This class has an internal hash table mapping source document names
464                 // to such ISymbolDocumentWriter instances - so there's exactly one
465                 // instance per document.
466                 //
467                 // This property returns the ISymbolDocumentWriter instance which belongs
468                 // to the location's source file.
469                 //
470                 // If we don't have a symbol writer, this property is always null.
471                 public SourceFile SourceFile {
472                         get {
473                                 int index = File;
474                                 if (index == 0)
475                                         return null;
476                                 return (SourceFile) source_list [index - 1];
477                         }
478                 }
479
480                 public CompilationUnit CompilationUnit {
481                         get {
482                                 int index = CompilationUnitIndex;
483                                 if (index == 0)
484                                         return null;
485                                 return (CompilationUnit) source_list [index - 1];
486                         }
487                 }
488
489                 #region IEquatable<Location> Members
490
491                 public bool Equals (Location other)
492                 {
493                         return this.token == other.token;
494                 }
495
496                 #endregion
497         }
498
499         //
500         // A bag of additional locations to support full ast tree
501         //
502         public class LocationsBag
503         {
504                 public class MemberLocations
505                 {
506                         public readonly IList<Tuple<Modifiers, Location>> Modifiers;
507                         Location[] locations;
508
509                         public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
510                         {
511                                 Modifiers = mods;
512                                 locations = locs;
513                         }
514
515                         #region Properties
516
517                         public Location this [int index] {
518                                 get {
519                                         return locations [index];
520                                 }
521                         }
522                         
523                         public int Count {
524                                 get {
525                                         return locations.Length;
526                                 }
527                         }
528
529                         #endregion
530
531                         public void AddLocations (params Location[] additional)
532                         {
533                                 if (locations == null) {
534                                         locations = additional;
535                                 } else {
536                                         int pos = locations.Length;
537                                         Array.Resize (ref locations, pos + additional.Length);
538                                         additional.CopyTo (locations, pos);
539                                 }
540                         }
541                 }
542
543                 Dictionary<object, Location[]> simple_locs = new Dictionary<object, Location[]> (ReferenceEquality<object>.Default);
544                 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
545
546                 [Conditional ("FULL_AST")]
547                 public void AddLocation (object element, params Location[] locations)
548                 {
549                         simple_locs.Add (element, locations);
550                 }
551
552                 [Conditional ("FULL_AST")]
553                 public void AddStatement (object element, params Location[] locations)
554                 {
555                         if (locations.Length == 0)
556                                 throw new ArgumentException ("Statement is missing semicolon location");
557
558                         simple_locs.Add (element, locations);
559                 }
560
561                 [Conditional ("FULL_AST")]
562                 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
563                 {
564                         member_locs.Add (member, new MemberLocations (modLocations, locations));
565                 }
566
567                 [Conditional ("FULL_AST")]
568                 public void AppendTo (object existing, params Location[] locations)
569                 {
570                         Location[] locs;
571                         if (simple_locs.TryGetValue (existing, out locs)) {
572                                 simple_locs [existing] = locs.Concat (locations).ToArray ();
573                                 return;
574                         }
575                 }
576
577                 [Conditional ("FULL_AST")]
578                 public void AppendToMember (MemberCore existing, params Location[] locations)
579                 {
580                         MemberLocations member;
581                         if (member_locs.TryGetValue (existing, out member)) {
582                                 member.AddLocations (locations);
583                                 return;
584                         }
585                 }
586
587                 public Location[] GetLocations (object element)
588                 {
589                         Location[] found;
590                         simple_locs.TryGetValue (element, out found);
591                         return found;
592                 }
593
594                 public MemberLocations GetMemberLocation (MemberCore element)
595                 {
596                         MemberLocations found;
597                         member_locs.TryGetValue (element, out found);
598                         return found;
599                 }
600         }
601 }