2008-07-04 Marek Safar <marek.safar@gmail.com>
[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 //
8 // Copyright 2001 Ximian, Inc.
9 // Copyright 2005 Novell, Inc.
10 //
11
12 using System;
13 using System.IO;
14 using System.Collections;
15 using Mono.CompilerServices.SymbolWriter;
16
17 namespace Mono.CSharp {
18         /// <summary>
19         ///   This is one single source file.
20         /// </summary>
21         /// <remarks>
22         ///   This is intentionally a class and not a struct since we need
23         ///   to pass this by reference.
24         /// </remarks>
25         public class SourceFile : ISourceFile {
26                 public readonly string Name;
27                 public readonly string Path;
28                 public readonly int Index;
29                 public bool AutoGenerated;
30                 public bool IsIncludeFile;
31
32                 SourceFileEntry file;
33                 byte[] guid, checksum;
34
35                 public SourceFile (string name, string path, int index, bool is_include)
36                 {
37                         this.Index = index;
38                         this.Name = name;
39                         this.Path = path;
40                         this.IsIncludeFile = is_include;
41                 }
42
43                 public SourceFileEntry SourceFileEntry {
44                         get { return file; }
45                 }
46
47                 SourceFileEntry ISourceFile.Entry {
48                         get { return file; }
49                 }
50
51                 public void SetChecksum (byte[] guid, byte[] checksum)
52                 {
53                         this.guid = guid;
54                         this.checksum = checksum;
55                 }
56
57                 public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter)
58                 {
59                         if (guid != null)
60                                 file = symwriter.DefineDocument (Path, guid, checksum);
61                         else {
62                                 file = symwriter.DefineDocument (Path);
63                                 if (AutoGenerated)
64                                         file.SetAutoGenerated ();
65                         }
66                 }
67
68                 public override string ToString ()
69                 {
70                         return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
71                                               Name, Path, Index, SourceFileEntry);
72                 }
73         }
74
75         public class CompilationUnit : SourceFile, ICompileUnit
76         {
77                 CompileUnitEntry comp_unit;
78                 Hashtable include_files;
79
80                 public CompilationUnit (string name, string path, int index)
81                         : base (name, path, index, false)
82                 { }
83
84                 public void AddFile (SourceFile file)
85                 {
86                         if (include_files == null)
87                                 include_files = new Hashtable ();
88
89                         if (!include_files.Contains (file.Path))
90                                 include_files.Add (file.Path, file);
91                 }
92
93                 CompileUnitEntry ICompileUnit.Entry {
94                         get { return comp_unit; }
95                 }
96
97                 public CompileUnitEntry CompileUnitEntry {
98                         get { return comp_unit; }
99                 }
100
101                 public override void DefineSymbolInfo (MonoSymbolWriter symwriter)
102                 {
103                         base.DefineSymbolInfo (symwriter);
104
105                         comp_unit = symwriter.DefineCompilationUnit (SourceFileEntry);
106
107                         if (include_files != null) {
108                                 foreach (SourceFile include in include_files.Values) {
109                                         include.DefineSymbolInfo (symwriter);
110                                         comp_unit.AddFile (include.SourceFileEntry);
111                                 }
112                         }
113                 }
114         }
115
116         /// <summary>
117         ///   Keeps track of the location in the program
118         /// </summary>
119         ///
120         /// <remarks>
121         ///   This uses a compact representation and a couple of auxiliary
122         ///   structures to keep track of tokens to (file,line and column) 
123         ///   mappings. The usage of the bits is:
124         ///   
125         ///     - 16 bits for "checkpoint" which is a mixed concept of
126         ///       file and "line segment"
127         ///     - 8 bits for line delta (offset) from the line segment
128         ///     - 8 bits for column number.
129         ///
130         ///   http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
131         /// </remarks>
132         public struct Location {
133                 int token; 
134
135                 struct Checkpoint {
136                         public readonly int LineOffset;
137                         public readonly int CompilationUnit;
138                         public readonly int File;
139
140                         public Checkpoint (int compile_unit, int file, int line)
141                         {
142                                 File = file;
143                                 CompilationUnit = compile_unit;
144                                 LineOffset = line - (int) (line % (1 << line_delta_bits));
145                         }
146                 }
147
148                 static ArrayList source_list;
149                 static ArrayList compile_units;
150                 static Hashtable source_files;
151                 static int checkpoint_bits;
152                 static int source_count;
153                 static int current_source;
154                 static int current_compile_unit;
155                 static int line_delta_bits;
156                 static int line_delta_mask;
157                 static int column_bits;
158                 static int column_mask;
159                 static Checkpoint [] checkpoints;
160                 static int checkpoint_index;
161                 
162                 public readonly static Location Null = new Location (-1);
163                 public static bool InEmacs;
164                 
165                 static Location ()
166                 {
167                         source_files = new Hashtable ();
168                         source_list = new ArrayList ();
169                         compile_units = new ArrayList ();
170                         current_source = 0;
171                         current_compile_unit = 0;
172                         checkpoints = new Checkpoint [10];
173                 }
174
175                 public static void Reset ()
176                 {
177                         source_files = new Hashtable ();
178                         source_list = new ArrayList ();
179                         compile_units = new ArrayList ();
180                         current_source = 0;
181                         current_compile_unit = 0;
182                         source_count = 0;
183                 }
184
185                 // <summary>
186                 //   This must be called before parsing/tokenizing any files.
187                 // </summary>
188                 static public void AddFile (string name)
189                 {
190                         string path = Path.GetFullPath (name);
191
192                         if (source_files.Contains (path)){
193                                 int id = (int) source_files [path];
194                                 string other_name = ((SourceFile) source_list [id - 1]).Name;
195                                 if (name.Equals (other_name))
196                                         Report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
197                                 else
198                                         Report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
199                                 return;
200                         }
201
202                         source_files.Add (path, ++source_count);
203                         CompilationUnit unit = new CompilationUnit (name, path, source_count);
204                         source_list.Add (unit);
205                         compile_units.Add (unit);
206                 }
207
208                 static public CompilationUnit[] SourceFiles {
209                         get {
210                                 CompilationUnit[] retval = new CompilationUnit [compile_units.Count];
211                                 compile_units.CopyTo (retval, 0);
212                                 return retval;
213                         }
214                 }
215
216                 // <summary>
217                 //   After adding all source files we want to compile with AddFile(), this method
218                 //   must be called to `reserve' an appropriate number of bits in the token for the
219                 //   source file.  We reserve some extra space for files we encounter via #line
220                 //   directives while parsing.
221                 // </summary>
222                 static public void Initialize ()
223                 {
224                         checkpoints = new Checkpoint [source_list.Count * 2];
225                         if (checkpoints.Length > 0)
226                                 checkpoints [0] = new Checkpoint (0, 0, 0);
227
228                         column_bits = 8;
229                         column_mask = 0xFF;
230                         line_delta_bits = 8;
231                         line_delta_mask = 0xFF00;
232                         checkpoint_index = 0;
233                         checkpoint_bits = 16;
234                 }
235
236                 // <remarks>
237                 //   This is used when we encounter a #line preprocessing directive.
238                 // </remarks>
239                 static public SourceFile LookupFile (CompilationUnit comp_unit, string name)
240                 {
241                         string path;
242                         if (!Path.IsPathRooted (name)) {
243                                 string root = Path.GetDirectoryName (comp_unit.Path);
244                                 path = Path.Combine (root, name);
245                         } else
246                                 path = name;
247
248                         if (!source_files.Contains (path)) {
249                                 if (source_count >= (1 << checkpoint_bits))
250                                         return new SourceFile (name, path, 0, true);
251
252                                 source_files.Add (path, ++source_count);
253                                 SourceFile retval = new SourceFile (name, path, source_count, true);
254                                 source_list.Add (retval);
255                                 return retval;
256                         }
257
258                         int index = (int) source_files [path];
259                         return (SourceFile) source_list [index - 1];
260                 }
261
262                 static public void Push (CompilationUnit compile_unit, SourceFile file)
263                 {
264                         current_source = file != null ? file.Index : -1;
265                         current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
266                         // File is always pushed before being changed.
267                 }
268
269                 // <remarks>
270                 //   If we're compiling with debugging support, this is called between parsing
271                 //   and code generation to register all the source files with the
272                 //   symbol writer.
273                 // </remarks>
274                 static public void DefineSymbolDocuments (MonoSymbolWriter symwriter)
275                 {
276                         foreach (CompilationUnit unit in compile_units)
277                                 unit.DefineSymbolInfo (symwriter);
278                 }
279                 
280                 public Location (int row)
281                         : this (row, 0)
282                 {
283                 }
284
285                 public Location (int row, int column)
286                 {
287                         if (row <= 0)
288                                 token = 0;
289                         else {
290                                 if (column > 254)
291                                         column = 254;
292                                 if (column < 0)
293                                         column = 255;
294                                 int target = -1;
295                                 int delta = 0;
296                                 int max = checkpoint_index < 10 ?
297                                         checkpoint_index : 10;
298                                 for (int i = 0; i < max; i++) {
299                                         int offset = checkpoints [checkpoint_index - i].LineOffset;
300                                         delta = row - offset;
301                                         if (delta >= 0 &&
302                                                 delta < (1 << line_delta_bits) &&
303                                                 checkpoints [checkpoint_index - i].File == current_source) {
304                                                 target = checkpoint_index - i;
305                                                 break;
306                                         }
307                                 }
308                                 if (target == -1) {
309                                         AddCheckpoint (current_compile_unit, current_source, row);
310                                         target = checkpoint_index;
311                                         delta = row % (1 << line_delta_bits);
312                                 }
313                                 long l = column +
314                                         (long) (delta << column_bits) +
315                                         (long) (target << (line_delta_bits + column_bits));
316                                 token = l > 0xFFFFFFFF ? 0 : (int) l;
317                         }
318                 }
319
320                 static void AddCheckpoint (int compile_unit, int file, int row)
321                 {
322                         if (checkpoints.Length == ++checkpoint_index) {
323                                 Checkpoint [] tmp = new Checkpoint [checkpoint_index * 2];
324                                 Array.Copy (checkpoints, tmp, checkpoints.Length);
325                                 checkpoints = tmp;
326                         }
327                         checkpoints [checkpoint_index] = new Checkpoint (compile_unit, file, row);
328                 }
329                 
330                 public override string ToString ()
331                 {
332                         if (column_bits == 0 || InEmacs)
333                                 return Name + "(" + Row.ToString () + "):";
334                         else
335                                 return Name + "(" + Row.ToString () + "," + Column.ToString () +
336                                         (Column == column_mask ? "+):" : "):");
337                 }
338                 
339                 /// <summary>
340                 ///   Whether the Location is Null
341                 /// </summary>
342                 public bool IsNull {
343                         get { return token == 0; }
344                 }
345
346                 public string Name {
347                         get {
348                                 int index = File;
349                                 if (token == 0 || index == 0)
350                                         return "Internal";
351
352                                 SourceFile file = (SourceFile) source_list [index - 1];
353                                 return file.Name;
354                         }
355                 }
356
357                 int CheckpointIndex {
358                         get { return (int) ((token & 0xFFFF0000) >> (line_delta_bits + column_bits)); }
359                 }
360
361                 public int Row {
362                         get {
363                                 if (token == 0)
364                                         return 1;
365                                 return checkpoints [CheckpointIndex].LineOffset + ((token & line_delta_mask) >> column_bits);
366                         }
367                 }
368
369                 public int Column {
370                         get {
371                                 if (token == 0)
372                                         return 1;
373                                 int col = (int) (token & column_mask);
374                                 return col == 255 ? 1 : col;
375                         }
376                 }
377
378                 public bool Hidden {
379                         get {
380                                 return (int) (token & column_mask) == 255;
381                         }
382                 }
383
384                 public int CompilationUnitIndex {
385                         get {
386                                 if (token == 0)
387                                         return 0;
388 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));
389                                 return checkpoints [CheckpointIndex].CompilationUnit;
390                         }
391                 }
392
393                 public int File {
394                         get {
395                                 if (token == 0)
396                                         return 0;
397 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));
398                                 return checkpoints [CheckpointIndex].File;
399                         }
400                 }
401
402                 // The ISymbolDocumentWriter interface is used by the symbol writer to
403                 // describe a single source file - for each source file there's exactly
404                 // one corresponding ISymbolDocumentWriter instance.
405                 //
406                 // This class has an internal hash table mapping source document names
407                 // to such ISymbolDocumentWriter instances - so there's exactly one
408                 // instance per document.
409                 //
410                 // This property returns the ISymbolDocumentWriter instance which belongs
411                 // to the location's source file.
412                 //
413                 // If we don't have a symbol writer, this property is always null.
414                 public SourceFile SourceFile {
415                         get {
416                                 int index = File;
417                                 if (index == 0)
418                                         return null;
419                                 return (SourceFile) source_list [index - 1];
420                         }
421                 }
422
423                 public CompilationUnit CompilationUnit {
424                         get {
425                                 int index = CompilationUnitIndex;
426                                 if (index == 0)
427                                         return null;
428                                 return (CompilationUnit) source_list [index - 1];
429                         }
430                 }
431         }
432
433         public class LocatedToken
434         {
435                 public readonly Location Location;
436                 public readonly string Value;
437
438                 public LocatedToken (Location loc, string value)
439                 {
440                         Location = loc;
441                         Value = value;
442                 }
443
444                 public override string ToString ()
445                 {
446                         return Location.ToString () + Value;
447                 }
448         }
449 }