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