2 // location.cs: Keeps track of the location of source code entity
7 // (C) 2001 Ximian, Inc.
12 using System.Collections;
13 using System.Diagnostics.SymbolStore;
15 namespace Mono.CSharp {
17 /// This is one single source file.
20 /// This is intentionally a class and not a struct since we need
21 /// to pass this by reference.
23 public sealed class SourceFile {
24 public readonly string Name;
25 public readonly string Path;
26 public readonly int Index;
27 public ISymbolDocumentWriter SymbolDocument;
29 public SourceFile (string name, string path, int index)
38 /// Keeps track of the location in the program
42 /// This uses a compact representation and a couple of auxiliary
43 /// structures to keep track of tokens to (file,line) mappings.
45 /// We could probably also keep track of columns by storing those
46 /// in 8 bits (and say, map anything after char 255 to be `255+').
48 public struct Location {
51 static ArrayList source_list;
52 static Hashtable source_files;
53 static int source_bits;
54 static int source_mask;
55 static int source_count;
56 static int current_source;
58 public readonly static Location Null;
62 source_files = new Hashtable ();
63 source_list = new ArrayList ();
69 // This must be called before parsing/tokenizing any files.
71 static public void AddFile (string name)
73 string path = Path.GetFullPath (name);
75 if (source_files.Contains (path)){
78 "Source file `" + name + "' specified multiple times");
82 source_files.Add (path, ++source_count);
83 source_list.Add (new SourceFile (name, path, source_count));
86 static public SourceFile[] SourceFiles {
88 SourceFile[] retval = new SourceFile [source_list.Count];
89 source_list.CopyTo (retval, 0);
94 static int log2 (int number)
106 // After adding all source files we want to compile with AddFile(), this method
107 // must be called to `reserve' an appropriate number of bits in the token for the
108 // source file. We reserve some extra space for files we encounter via #line
109 // directives while parsing.
111 static public void Initialize ()
113 source_bits = log2 (source_list.Count) + 2;
114 source_mask = (1 << source_bits) - 1;
118 // This is used when we encounter a #line preprocessing directive.
120 static public SourceFile LookupFile (string name)
122 string path = Path.GetFullPath (name);
124 if (!source_files.Contains (path)) {
125 if (source_count >= (1 << source_bits))
126 return new SourceFile (name, path, 0);
128 source_files.Add (path, ++source_count);
129 SourceFile retval = new SourceFile (name, path, source_count);
130 source_list.Add (retval);
134 int index = (int) source_files [path];
135 return (SourceFile) source_list [index - 1];
138 static public void Push (SourceFile file)
140 current_source = file.Index;
144 // If we're compiling with debugging support, this is called between parsing and
145 // code generation to register all the source files with the symbol writer. //
147 static public void DefineSymbolDocuments (SymbolWriter symwriter)
149 foreach (SourceFile file in source_list)
150 file.SymbolDocument = symwriter.DefineDocument (file.Path);
153 public Location (int row)
158 token = current_source + (row << source_bits);
161 public override string ToString ()
163 return Name + ": (" + Row + ")";
167 /// Whether the Location is Null
169 static public bool IsNull (Location l)
176 int index = token & source_mask;
177 if ((token == 0) || (index == 0))
180 SourceFile file = (SourceFile) source_list [index - 1];
190 return token >> source_bits;
196 return token & source_mask;
200 // The ISymbolDocumentWriter interface is used by the symbol writer to
201 // describe a single source file - for each source file there's exactly
202 // one corresponding ISymbolDocumentWriter instance.
204 // This class has an internal hash table mapping source document names
205 // to such ISymbolDocumentWriter instances - so there's exactly one
206 // instance per document.
208 // This property returns the ISymbolDocumentWriter instance which belongs
209 // to the location's source file.
211 // If we don't have a symbol writer, this property is always null.
212 public ISymbolDocumentWriter SymbolDocument {
214 int index = token & source_mask;
217 SourceFile file = (SourceFile) source_list [index - 1];
218 return file.SymbolDocument;