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