2 // location.cs: Keeps track of the location of source code entity
6 // Atsushi Enomoto <atsushi@ximian.com>
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright 2001 Ximian, Inc.
10 // Copyright 2005 Novell, Inc.
15 using System.Collections.Generic;
16 using Mono.CompilerServices.SymbolWriter;
17 using System.Diagnostics;
20 namespace Mono.CSharp {
22 /// This is one single source file.
25 /// This is intentionally a class and not a struct since we need
26 /// to pass this by reference.
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;
36 byte[] guid, checksum;
38 public SourceFile (string name, string path, int index, bool is_include)
43 this.IsIncludeFile = is_include;
46 public SourceFileEntry SourceFileEntry {
50 SourceFileEntry ISourceFile.Entry {
54 public void SetChecksum (byte[] guid, byte[] checksum)
57 this.checksum = checksum;
60 public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter)
63 file = symwriter.DefineDocument (Path, guid, checksum);
65 file = symwriter.DefineDocument (Path);
67 file.SetAutoGenerated ();
71 public override string ToString ()
73 return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
74 Name, Path, Index, SourceFileEntry);
78 public class CompilationUnit : SourceFile, ICompileUnit
80 CompileUnitEntry comp_unit;
81 Dictionary<string, SourceFile> include_files;
82 Dictionary<string, bool> conditionals;
84 public CompilationUnit (string name, string path, int index)
85 : base (name, path, index, false)
88 public void AddFile (SourceFile file)
93 if (include_files == null)
94 include_files = new Dictionary<string, SourceFile> ();
96 if (!include_files.ContainsKey (file.Path))
97 include_files.Add (file.Path, file);
100 public void AddDefine (string value)
102 if (conditionals == null)
103 conditionals = new Dictionary<string, bool> (2);
105 conditionals [value] = true;
108 public void AddUndefine (string value)
110 if (conditionals == null)
111 conditionals = new Dictionary<string, bool> (2);
113 conditionals [value] = false;
116 CompileUnitEntry ICompileUnit.Entry {
117 get { return comp_unit; }
120 public CompileUnitEntry CompileUnitEntry {
121 get { return comp_unit; }
124 public override void DefineSymbolInfo (MonoSymbolWriter symwriter)
126 base.DefineSymbolInfo (symwriter);
128 comp_unit = symwriter.DefineCompilationUnit (SourceFileEntry);
130 if (include_files != null) {
131 foreach (SourceFile include in include_files.Values) {
132 include.DefineSymbolInfo (symwriter);
133 comp_unit.AddFile (include.SourceFileEntry);
138 public bool IsConditionalDefined (string value)
140 if (conditionals != null) {
142 if (conditionals.TryGetValue (value, out res))
145 // When conditional was undefined
146 if (conditionals.ContainsKey (value))
150 return RootContext.IsConditionalDefined (value);
155 /// Keeps track of the location in the program
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:
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.
168 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
170 public struct Location {
174 public readonly int LineOffset;
175 public readonly int CompilationUnit;
176 public readonly int File;
178 public Checkpoint (int compile_unit, int file, int line)
181 CompilationUnit = compile_unit;
182 LineOffset = line - (int) (line % (1 << line_delta_bits));
186 static List<SourceFile> source_list;
187 static List<CompilationUnit> compile_units;
188 static Dictionary<string, int> source_files;
189 static int checkpoint_bits;
190 static int source_count;
191 static int current_source;
192 static int current_compile_unit;
193 static int line_delta_bits;
194 static int line_delta_mask;
195 static int column_bits;
196 static int column_mask;
197 static Checkpoint [] checkpoints;
198 static int checkpoint_index;
200 public readonly static Location Null = new Location (-1);
201 public static bool InEmacs;
206 checkpoints = new Checkpoint [10];
209 public static void Reset ()
211 source_files = new Dictionary<string, int> ();
212 source_list = new List<SourceFile> ();
213 compile_units = new List<CompilationUnit> ();
215 current_compile_unit = 0;
220 // This must be called before parsing/tokenizing any files.
222 static public void AddFile (Report r, string name)
224 string path = Path.GetFullPath (name);
226 if (source_files.TryGetValue (path, out id)){
227 string other_name = source_list [id - 1].Name;
228 if (name.Equals (other_name))
229 r.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
231 r.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
235 source_files.Add (path, ++source_count);
236 CompilationUnit unit = new CompilationUnit (name, path, source_count);
237 source_list.Add (unit);
238 compile_units.Add (unit);
241 public static IList<CompilationUnit> SourceFiles {
243 return compile_units;
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.
253 static public void Initialize ()
255 checkpoints = new Checkpoint [source_list.Count * 2];
256 if (checkpoints.Length > 0)
257 checkpoints [0] = new Checkpoint (0, 0, 0);
262 line_delta_mask = 0xFF00;
263 checkpoint_index = 0;
264 checkpoint_bits = 16;
268 // This is used when we encounter a #line preprocessing directive.
270 static public SourceFile LookupFile (CompilationUnit comp_unit, string name)
273 if (!Path.IsPathRooted (name)) {
274 string root = Path.GetDirectoryName (comp_unit.Path);
275 path = Path.Combine (root, name);
279 if (!source_files.ContainsKey (path)) {
280 if (source_count >= (1 << checkpoint_bits))
281 return new SourceFile (name, path, 0, true);
283 source_files.Add (path, ++source_count);
284 SourceFile retval = new SourceFile (name, path, source_count, true);
285 source_list.Add (retval);
289 int index = (int) source_files [path];
290 return (SourceFile) source_list [index - 1];
293 static public void Push (CompilationUnit compile_unit, SourceFile file)
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.
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
305 static public void DefineSymbolDocuments (MonoSymbolWriter symwriter)
307 foreach (CompilationUnit unit in compile_units)
308 unit.DefineSymbolInfo (symwriter);
311 public Location (int row)
316 public Location (int row, int column)
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;
333 delta < (1 << line_delta_bits) &&
334 checkpoints [checkpoint_index - i].File == current_source) {
335 target = checkpoint_index - i;
340 AddCheckpoint (current_compile_unit, current_source, row);
341 target = checkpoint_index;
342 delta = row % (1 << line_delta_bits);
345 (long) (delta << column_bits) +
346 (long) (target << (line_delta_bits + column_bits));
347 token = l > 0xFFFFFFFF ? 0 : (int) l;
351 public static Location operator - (Location loc, int columns)
353 return new Location (loc.Row, loc.Column - columns);
356 static void AddCheckpoint (int compile_unit, int file, int row)
358 if (checkpoints.Length == ++checkpoint_index) {
359 Checkpoint [] tmp = new Checkpoint [checkpoint_index * 2];
360 Array.Copy (checkpoints, tmp, checkpoints.Length);
363 checkpoints [checkpoint_index] = new Checkpoint (compile_unit, file, row);
366 public override string ToString ()
368 if (column_bits == 0 || InEmacs)
369 return Name + "(" + Row.ToString () + "):";
371 return Name + "(" + Row.ToString () + "," + Column.ToString () +
372 (Column == column_mask ? "+):" : "):");
376 /// Whether the Location is Null
379 get { return token == 0; }
385 if (token == 0 || index == 0)
388 SourceFile file = (SourceFile) source_list [index - 1];
393 int CheckpointIndex {
394 get { return (int) ((token & 0xFFFF0000) >> (line_delta_bits + column_bits)); }
401 return checkpoints [CheckpointIndex].LineOffset + ((token & line_delta_mask) >> column_bits);
409 int col = (int) (token & column_mask);
410 return col == 255 ? 1 : col;
416 return (int) (token & column_mask) == 255;
420 public int CompilationUnitIndex {
424 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));
425 return checkpoints [CheckpointIndex].CompilationUnit;
433 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));
434 return checkpoints [CheckpointIndex].File;
438 // The ISymbolDocumentWriter interface is used by the symbol writer to
439 // describe a single source file - for each source file there's exactly
440 // one corresponding ISymbolDocumentWriter instance.
442 // This class has an internal hash table mapping source document names
443 // to such ISymbolDocumentWriter instances - so there's exactly one
444 // instance per document.
446 // This property returns the ISymbolDocumentWriter instance which belongs
447 // to the location's source file.
449 // If we don't have a symbol writer, this property is always null.
450 public SourceFile SourceFile {
455 return (SourceFile) source_list [index - 1];
459 public CompilationUnit CompilationUnit {
461 int index = CompilationUnitIndex;
464 return (CompilationUnit) source_list [index - 1];
470 // A bag of additional locations to support full ast tree
472 public class LocationsBag
474 public class MemberLocations
476 public readonly IList<Tuple<Modifiers, Location>> Modifiers;
477 Location[] locations;
479 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
487 public Location this [int index] {
489 return locations [index];
495 return locations.Length;
501 public void AddLocations (params Location[] additional)
503 if (locations == null) {
504 locations = additional;
506 int pos = locations.Length;
507 Array.Resize (ref locations, pos + additional.Length);
508 additional.CopyTo (locations, pos);
513 Dictionary<object, Location[]> simple_locs = new Dictionary<object, Location[]> (ReferenceEquality<object>.Default);
514 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
516 [Conditional ("FULL_AST")]
517 public void AddLocation (object element, params Location[] locations)
519 simple_locs.Add (element, locations);
522 [Conditional ("FULL_AST")]
523 public void AddStatement (object element, params Location[] locations)
525 if (locations.Length == 0)
526 throw new ArgumentException ("Statement is missing semicolon location");
528 simple_locs.Add (element, locations);
531 [Conditional ("FULL_AST")]
532 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
534 member_locs.Add (member, new MemberLocations (modLocations, locations));
537 [Conditional ("FULL_AST")]
538 public void AppendTo (object existing, params Location[] locations)
541 if (simple_locs.TryGetValue (existing, out locs)) {
542 simple_locs [existing] = locs.Concat (locations).ToArray ();
547 [Conditional ("FULL_AST")]
548 public void AppendToMember (MemberCore existing, params Location[] locations)
550 MemberLocations member;
551 if (member_locs.TryGetValue (existing, out member)) {
552 member.AddLocations (locations);
557 public Location[] GetLocations (object element)
560 simple_locs.TryGetValue (element, out found);
564 public MemberLocations GetMemberLocation (MemberCore element)
566 MemberLocations found;
567 member_locs.TryGetValue (element, out found);