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, IEquatable<SourceFile>
30 public readonly string Name;
31 public readonly string FullPathName;
32 public readonly int Index;
33 public bool AutoGenerated;
36 byte[] guid, checksum;
38 public SourceFile (string name, string path, int index)
42 this.FullPathName = path;
45 public SourceFileEntry SourceFileEntry {
49 SourceFileEntry ISourceFile.Entry {
53 public void SetChecksum (byte[] guid, byte[] checksum)
56 this.checksum = checksum;
59 public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter)
62 file = symwriter.DefineDocument (FullPathName, guid, checksum);
64 file = symwriter.DefineDocument (FullPathName);
66 file.SetAutoGenerated ();
70 public bool Equals (SourceFile other)
72 return FullPathName == other.FullPathName;
75 public override string ToString ()
77 return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
78 Name, FullPathName, Index, SourceFileEntry);
82 public class CompilationSourceFile : SourceFile, ICompileUnit
84 CompileUnitEntry comp_unit;
85 Dictionary<string, SourceFile> include_files;
86 Dictionary<string, bool> conditionals;
87 NamespaceEntry ns_container;
89 public CompilationSourceFile (string name, string fullPathName, int index)
90 : base (name, fullPathName, index)
94 CompileUnitEntry ICompileUnit.Entry {
95 get { return comp_unit; }
98 public CompileUnitEntry CompileUnitEntry {
99 get { return comp_unit; }
102 public NamespaceEntry NamespaceContainer {
107 ns_container = value;
111 public void AddIncludeFile (SourceFile file)
116 if (include_files == null)
117 include_files = new Dictionary<string, SourceFile> ();
119 if (!include_files.ContainsKey (file.FullPathName))
120 include_files.Add (file.FullPathName, file);
123 public void AddDefine (string value)
125 if (conditionals == null)
126 conditionals = new Dictionary<string, bool> (2);
128 conditionals [value] = true;
131 public void AddUndefine (string value)
133 if (conditionals == null)
134 conditionals = new Dictionary<string, bool> (2);
136 conditionals [value] = false;
139 public override void DefineSymbolInfo (MonoSymbolWriter symwriter)
141 base.DefineSymbolInfo (symwriter);
143 comp_unit = symwriter.DefineCompilationUnit (SourceFileEntry);
145 if (include_files != null) {
146 foreach (SourceFile include in include_files.Values) {
147 include.DefineSymbolInfo (symwriter);
148 comp_unit.AddFile (include.SourceFileEntry);
153 public bool IsConditionalDefined (CompilerContext ctx, string value)
155 if (conditionals != null) {
157 if (conditionals.TryGetValue (value, out res))
160 // When conditional was undefined
161 if (conditionals.ContainsKey (value))
165 return ctx.Settings.IsConditionalSymbolDefined (value);
170 /// Keeps track of the location in the program
174 /// This uses a compact representation and a couple of auxiliary
175 /// structures to keep track of tokens to (file,line and column)
176 /// mappings. The usage of the bits is:
178 /// - 16 bits for "checkpoint" which is a mixed concept of
179 /// file and "line segment"
180 /// - 8 bits for line delta (offset) from the line segment
181 /// - 8 bits for column number.
183 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
185 public struct Location : IEquatable<Location>
188 public readonly int LineOffset;
189 public readonly int CompilationUnit;
190 public readonly int File;
192 public Checkpoint (int compile_unit, int file, int line)
195 CompilationUnit = compile_unit;
196 LineOffset = line - (int) (line % (1 << line_delta_bits));
203 const int column_bits = 24;
204 const int line_delta_bits = 24;
208 const int column_bits = 8;
209 const int line_delta_bits = 8;
211 const int checkpoint_bits = 16;
213 // -2 because the last one is used for hidden
214 const int max_column = (1 << column_bits) - 2;
215 const int column_mask = (1 << column_bits) - 1;
217 static List<SourceFile> source_list;
218 static int current_source;
219 static int current_compile_unit;
220 static Checkpoint [] checkpoints;
221 static int checkpoint_index;
223 public readonly static Location Null = new Location (-1);
224 public static bool InEmacs;
231 public static void Reset ()
233 source_list = new List<SourceFile> ();
235 current_compile_unit = 0;
236 checkpoint_index = 0;
239 public static SourceFile AddFile (string name, string fullName)
241 var source = new SourceFile (name, fullName, source_list.Count + 1);
242 source_list.Add (source);
247 // After adding all source files we want to compile with AddFile(), this method
248 // must be called to `reserve' an appropriate number of bits in the token for the
249 // source file. We reserve some extra space for files we encounter via #line
250 // directives while parsing.
252 static public void Initialize (List<CompilationSourceFile> files)
255 source_list.AddRange (files);
257 source_list.AddRange (files.ToArray ());
260 checkpoints = new Checkpoint [source_list.Count * 2];
261 if (checkpoints.Length > 0)
262 checkpoints [0] = new Checkpoint (0, 0, 0);
265 static public void Push (CompilationSourceFile compile_unit, SourceFile file)
267 current_source = file != null ? file.Index : -1;
268 current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
269 // File is always pushed before being changed.
272 public Location (int row)
277 public Location (int row, int column)
282 if (column > max_column)
285 column = max_column + 1;
290 // FIXME: This value is certainly wrong but what was the intension
291 int max = checkpoint_index < 10 ?
292 checkpoint_index : 10;
293 for (int i = 0; i < max; i++) {
294 int offset = checkpoints [checkpoint_index - i].LineOffset;
295 delta = row - offset;
297 delta < (1 << line_delta_bits) &&
298 checkpoints [checkpoint_index - i].File == current_source) {
299 target = checkpoint_index - i;
304 AddCheckpoint (current_compile_unit, current_source, row);
305 target = checkpoint_index;
306 delta = row % (1 << line_delta_bits);
310 (delta << column_bits) +
311 (target << (line_delta_bits + column_bits));
315 token = l > 0xFFFFFFFF ? 0 : (int) l;
320 public static Location operator - (Location loc, int columns)
322 return new Location (loc.Row, loc.Column - columns);
325 static void AddCheckpoint (int compile_unit, int file, int row)
327 if (checkpoints.Length == ++checkpoint_index) {
328 Array.Resize (ref checkpoints, checkpoint_index * 2);
330 checkpoints [checkpoint_index] = new Checkpoint (compile_unit, file, row);
333 string FormatLocation (string fileName)
335 if (column_bits == 0 || InEmacs)
336 return fileName + "(" + Row.ToString () + "):";
338 return fileName + "(" + Row.ToString () + "," + Column.ToString () +
339 (Column == max_column ? "+):" : "):");
342 public override string ToString ()
344 return FormatLocation (Name);
347 public string ToStringFullName ()
349 return FormatLocation (NameFullPath);
353 /// Whether the Location is Null
356 get { return token == 0; }
362 if (token == 0 || index == 0)
365 SourceFile file = source_list [index - 1];
370 public string NameFullPath {
373 if (token == 0 || index == 0)
376 return source_list[index - 1].FullPathName;
380 int CheckpointIndex {
382 const int checkpoint_mask = (1 << checkpoint_bits) - 1;
383 return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
392 int offset = checkpoints[CheckpointIndex].LineOffset;
394 const int line_delta_mask = (1 << column_bits) - 1;
395 return offset + (((int)(token >> column_bits)) & line_delta_mask);
403 int col = (int) (token & column_mask);
404 return col > max_column ? 1 : col;
410 return (int) (token & column_mask) == max_column + 1;
414 public int CompilationUnitIndex {
418 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));
419 return checkpoints [CheckpointIndex].CompilationUnit;
427 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));
428 return checkpoints [CheckpointIndex].File;
432 // The ISymbolDocumentWriter interface is used by the symbol writer to
433 // describe a single source file - for each source file there's exactly
434 // one corresponding ISymbolDocumentWriter instance.
436 // This class has an internal hash table mapping source document names
437 // to such ISymbolDocumentWriter instances - so there's exactly one
438 // instance per document.
440 // This property returns the ISymbolDocumentWriter instance which belongs
441 // to the location's source file.
443 // If we don't have a symbol writer, this property is always null.
444 public SourceFile SourceFile {
449 return (SourceFile) source_list [index - 1];
453 public CompilationSourceFile CompilationUnit {
455 int index = CompilationUnitIndex;
458 return (CompilationSourceFile) source_list [index - 1];
462 #region IEquatable<Location> Members
464 public bool Equals (Location other)
466 return this.token == other.token;
473 // A bag of additional locations to support full ast tree
475 public class LocationsBag
477 public class MemberLocations
479 public readonly IList<Tuple<Modifiers, Location>> Modifiers;
480 Location[] locations;
482 public MemberLocations (IList<Tuple<Modifiers, Location>> mods, Location[] locs)
490 public Location this [int index] {
492 return locations [index];
498 return locations.Length;
504 public void AddLocations (params Location[] additional)
506 if (locations == null) {
507 locations = additional;
509 int pos = locations.Length;
510 Array.Resize (ref locations, pos + additional.Length);
511 additional.CopyTo (locations, pos);
516 Dictionary<object, Location[]> simple_locs = new Dictionary<object, Location[]> (ReferenceEquality<object>.Default);
517 Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
519 [Conditional ("FULL_AST")]
520 public void AddLocation (object element, params Location[] locations)
522 simple_locs.Add (element, locations);
525 [Conditional ("FULL_AST")]
526 public void AddStatement (object element, params Location[] locations)
528 if (locations.Length == 0)
529 throw new ArgumentException ("Statement is missing semicolon location");
531 simple_locs.Add (element, locations);
534 [Conditional ("FULL_AST")]
535 public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
537 member_locs.Add (member, new MemberLocations (modLocations, locations));
540 [Conditional ("FULL_AST")]
541 public void AppendTo (object existing, params Location[] locations)
544 if (simple_locs.TryGetValue (existing, out locs)) {
545 simple_locs [existing] = locs.Concat (locations).ToArray ();
550 [Conditional ("FULL_AST")]
551 public void AppendToMember (MemberCore existing, params Location[] locations)
553 MemberLocations member;
554 if (member_locs.TryGetValue (existing, out member)) {
555 member.AddLocations (locations);
560 public Location[] GetLocations (object element)
563 simple_locs.TryGetValue (element, out found);
567 public MemberLocations GetMemberLocation (MemberCore element)
569 MemberLocations found;
570 member_locs.TryGetValue (element, out found);