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;
28 public bool HasLineDirective;
30 public SourceFile (string name, string path, int index)
39 /// Keeps track of the location in the program
43 /// This uses a compact representation and a couple of auxiliary
44 /// structures to keep track of tokens to (file,line) mappings.
46 /// We could probably also keep track of columns by storing those
47 /// in 8 bits (and say, map anything after char 255 to be `255+').
49 public struct Location {
52 static ArrayList source_list;
53 static Hashtable source_files;
54 static int source_bits;
55 static int source_mask;
56 static int source_count;
57 static int current_source;
59 public readonly static Location Null;
63 source_files = new Hashtable ();
64 source_list = new ArrayList ();
70 // This must be called before parsing/tokenizing any files.
72 static public void AddFile (string name)
74 string path = Path.GetFullPath (name);
76 if (source_files.Contains (path)){
79 "Source file `" + name + "' specified multiple times");
83 source_files.Add (path, ++source_count);
84 source_list.Add (new SourceFile (name, path, source_count));
87 static public SourceFile[] SourceFiles {
89 SourceFile[] retval = new SourceFile [source_list.Count];
90 source_list.CopyTo (retval, 0);
95 static int log2 (int number)
107 // After adding all source files we want to compile with AddFile(), this method
108 // must be called to `reserve' an appropriate number of bits in the token for the
109 // source file. We reserve some extra space for files we encounter via #line
110 // directives while parsing.
112 static public void Initialize ()
114 source_bits = log2 (source_list.Count) + 2;
115 source_mask = (1 << source_bits) - 1;
119 // This is used when we encounter a #line preprocessing directive.
121 static public SourceFile LookupFile (string name)
123 string path = name == "" ? "" : Path.GetFullPath (name);
125 if (!source_files.Contains (path)) {
126 if (source_count >= (1 << source_bits))
127 return new SourceFile (name, path, 0);
129 source_files.Add (path, ++source_count);
130 SourceFile retval = new SourceFile (name, path, source_count);
131 source_list.Add (retval);
135 int index = (int) source_files [path];
136 return (SourceFile) source_list [index - 1];
139 static public void Push (SourceFile file)
141 current_source = file.Index;
145 // If we're compiling with debugging support, this is called between parsing and
146 // code generation to register all the source files with the symbol writer. //
148 static public void DefineSymbolDocuments (SymbolWriter symwriter)
150 foreach (SourceFile file in source_list) {
151 if (file.HasLineDirective)
153 file.SymbolDocument = symwriter.DefineDocument (file.Path);
157 public Location (int row)
162 token = current_source + (row << source_bits);
165 public override string ToString ()
167 return Name + ": (" + Row + ")";
171 /// Whether the Location is Null
173 static public bool IsNull (Location l)
180 int index = token & source_mask;
181 if ((token == 0) || (index == 0))
184 SourceFile file = (SourceFile) source_list [index - 1];
194 return token >> source_bits;
200 return token & source_mask;
204 // The ISymbolDocumentWriter interface is used by the symbol writer to
205 // describe a single source file - for each source file there's exactly
206 // one corresponding ISymbolDocumentWriter instance.
208 // This class has an internal hash table mapping source document names
209 // to such ISymbolDocumentWriter instances - so there's exactly one
210 // instance per document.
212 // This property returns the ISymbolDocumentWriter instance which belongs
213 // to the location's source file.
215 // If we don't have a symbol writer, this property is always null.
216 public ISymbolDocumentWriter SymbolDocument {
218 int index = token & source_mask;
221 SourceFile file = (SourceFile) source_list [index - 1];
222 return file.SymbolDocument;