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