2 // location.cs: Keeps track of the location of source code entity
\r
7 // (C) 2001 Ximian, Inc.
\r
12 using System.Collections;
\r
13 using System.Diagnostics.SymbolStore;
\r
15 namespace Mono.MonoBASIC {
\r
18 /// Keeps track of the location in the program
\r
22 /// This uses a compact representation and a couple of auxiliary
\r
23 /// structures to keep track of tokens to (line, col, file) mappings.
\r
25 public struct Location {
\r
26 const int NUM_ROW_BITS = 16;
\r
27 const int NUM_COL_BITS = 8;
\r
28 const int NUM_FILE_BITS = (32-NUM_ROW_BITS-NUM_COL_BITS);
\r
30 const int NUM_FILE_SHIFTS = 0;
\r
31 const int NUM_ROW_SHIFTS = NUM_FILE_BITS;
\r
32 const int NUM_COL_SHIFTS = NUM_ROW_BITS+NUM_FILE_BITS;
\r
34 const int FILE_MASK = (1<<NUM_FILE_BITS)-1;
\r
35 const int ROW_MASK = ((1<<NUM_ROW_BITS)-1)<<NUM_FILE_BITS;
\r
36 const int COL_MASK = ((1<<NUM_COL_BITS)-1)<<(NUM_ROW_BITS+NUM_FILE_BITS);
\r
38 public int token; // ordered triplet: (Row, Col, File Index)
\r
40 static ArrayList source_list;
\r
41 static Hashtable source_files;
\r
42 static int source_count;
\r
43 static int current_source;
\r
44 static Hashtable sym_docs;
\r
46 public readonly static Location Null;
\r
50 source_files = new Hashtable ();
\r
51 source_list = new ArrayList ();
\r
53 sym_docs = new Hashtable ();
\r
57 static public void SetCurrentSource(string name)
\r
61 if (!source_files.Contains (name)) {
\r
62 index = ++source_count;
\r
63 source_files.Add (name, index);
\r
64 source_list.Add (name);
\r
67 index = (int)source_files[name];
\r
70 current_source = index;
\r
73 public Location (int row, int col)
\r
75 if (row < 0 || col < 0)
\r
80 token = (current_source<<NUM_FILE_SHIFTS) + (row<<NUM_ROW_SHIFTS) + (col<<NUM_COL_SHIFTS);
\r
84 public override string ToString ()
\r
86 return Name + ": (" + Row + ")";
\r
89 static public bool IsNull (Location l)
\r
91 return l.token == 0;
\r
94 public string Name {
\r
99 int index = (token & FILE_MASK)>>NUM_FILE_SHIFTS;
\r
100 string file = (string) source_list [index - 1];
\r
110 return (token & ROW_MASK)>>NUM_ROW_SHIFTS;
\r
119 return (token & COL_MASK)>>NUM_COL_SHIFTS;
\r
123 // The ISymbolDocumentWriter interface is used by the symbol writer to
\r
124 // describe a single source file - for each source file there's exactly
\r
125 // one corresponding ISymbolDocumentWriter instance.
\r
127 // This class has an internal hash table mapping source document names
\r
128 // to such ISymbolDocumentWriter instances - so there's exactly one
\r
129 // instance per document.
\r
131 // This property returns the ISymbolDocumentWriter instance which belongs
\r
132 // to the location's source file.
\r
134 // If we don't have a symbol writer, this property is always null.
\r
135 public ISymbolDocumentWriter SymbolDocument {
\r
137 ISymbolWriter sw = CodeGen.SymbolWriter;
\r
138 ISymbolDocumentWriter doc;
\r
143 // If we don't have a symbol writer, return null.
\r
147 string path = Path.GetFullPath (Name);
\r
149 if (sym_docs.Contains (path))
\r
150 // If we already created an ISymbolDocumentWriter
\r
151 // instance for this document, return it.
\r
152 doc = (ISymbolDocumentWriter) sym_docs [path];
\r
154 // Create a new ISymbolDocumentWriter instance and
\r
155 // store it in the hash table.
\r
156 doc = sw.DefineDocument (path, SymLanguageType.Basic,
\r
157 SymLanguageVendor.Microsoft,
\r
158 SymDocumentType.Text);
\r
160 sym_docs.Add (path, doc);
\r