2 // location.cs: Keeps track of the location of source code entity
7 // (C) 2001 Ximian, Inc.
12 using System.Collections;
13 using Mono.CompilerServices.SymbolWriter;
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 : ISourceFile {
24 public readonly string Name;
25 public readonly string Path;
26 public readonly int Index;
27 public SourceFileEntry SourceFileEntry;
28 public bool HasLineDirective;
30 public SourceFile (string name, string path, int index)
37 SourceFileEntry ISourceFile.Entry {
38 get { return SourceFileEntry; }
41 public override string ToString ()
43 return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
44 Name, Path, Index, SourceFileEntry);
49 /// Keeps track of the location in the program
53 /// This uses a compact representation and a couple of auxiliary
54 /// structures to keep track of tokens to (file,line) mappings.
56 /// We could probably also keep track of columns by storing those
57 /// in 8 bits (and say, map anything after char 255 to be `255+').
59 public struct Location {
62 static ArrayList source_list;
63 static Hashtable source_files;
64 static int source_bits;
65 static int source_mask;
66 static int source_count;
67 static int current_source;
69 public readonly static Location Null = new Location (-1);
73 source_files = new Hashtable ();
74 source_list = new ArrayList ();
78 public static void Reset ()
80 source_files = new Hashtable ();
81 source_list = new ArrayList ();
87 // This must be called before parsing/tokenizing any files.
89 static public void AddFile (string name)
91 string path = Path.GetFullPath (name);
93 if (source_files.Contains (path)){
94 int id = (int) source_files [path];
95 string other_name = ((SourceFile) source_list [id - 1]).Name;
96 if (name.Equals (other_name))
97 Report.Warning (2002, "Source file `{0}' specified multiple times", name);
99 Report.Warning (2002, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name, other_name, path);
103 source_files.Add (path, ++source_count);
104 source_list.Add (new SourceFile (name, path, source_count));
107 static public SourceFile[] SourceFiles {
109 SourceFile[] retval = new SourceFile [source_list.Count];
110 source_list.CopyTo (retval, 0);
115 static int log2 (int number)
127 // After adding all source files we want to compile with AddFile(), this method
128 // must be called to `reserve' an appropriate number of bits in the token for the
129 // source file. We reserve some extra space for files we encounter via #line
130 // directives while parsing.
132 static public void Initialize ()
134 source_bits = log2 (source_list.Count) + 2;
135 source_mask = (1 << source_bits) - 1;
139 // This is used when we encounter a #line preprocessing directive.
141 static public SourceFile LookupFile (string name)
143 string path = name == "" ? "" : Path.GetFullPath (name);
145 if (!source_files.Contains (path)) {
146 if (source_count >= (1 << source_bits))
147 return new SourceFile (name, path, 0);
149 source_files.Add (path, ++source_count);
150 SourceFile retval = new SourceFile (name, path, source_count);
151 source_list.Add (retval);
155 int index = (int) source_files [path];
156 return (SourceFile) source_list [index - 1];
159 static public void Push (SourceFile file)
161 current_source = file.Index;
165 // If we're compiling with debugging support, this is called between parsing
166 // and code generation to register all the source files with the
169 static public void DefineSymbolDocuments (SymbolWriter symwriter)
171 foreach (SourceFile file in source_list) {
172 file.SourceFileEntry = symwriter.DefineDocument (file.Path);
176 public Location (int row)
181 token = current_source + (row << source_bits);
184 public override string ToString ()
186 return Name + ": (" + Row + ")";
190 /// Whether the Location is Null
192 static public bool IsNull (Location l)
199 int index = token & source_mask;
200 if ((token == 0) || (index == 0))
203 SourceFile file = (SourceFile) source_list [index - 1];
213 return token >> source_bits;
219 return token & source_mask;
223 // The ISymbolDocumentWriter interface is used by the symbol writer to
224 // describe a single source file - for each source file there's exactly
225 // one corresponding ISymbolDocumentWriter instance.
227 // This class has an internal hash table mapping source document names
228 // to such ISymbolDocumentWriter instances - so there's exactly one
229 // instance per document.
231 // This property returns the ISymbolDocumentWriter instance which belongs
232 // to the location's source file.
234 // If we don't have a symbol writer, this property is always null.
235 public SourceFile SourceFile {
237 int index = token & source_mask;
240 return (SourceFile) source_list [index - 1];