**** Merged from MCS ****
[mono-project.git] / mcs / mbas / location.cs
blobc930229c789c4749dff2b53b68e18e342c1aa327
1 //
2 // location.cs: Keeps track of the location of source code entity
3 //
4 // Author:
5 // Miguel de Icaza
6 //
7 // (C) 2001 Ximian, Inc.
8 //
10 using System;
11 using System.IO;
12 using System.Collections;
13 using System.Diagnostics.SymbolStore;
15 namespace Mono.MonoBASIC {
17 /// <summary>
18 /// Keeps track of the location in the program
19 /// </summary>
20 ///
21 /// <remarks>
22 /// This uses a compact representation and a couple of auxiliary
23 /// structures to keep track of tokens to (line, col, file) mappings.
24 ///
25 public struct Location {
26 const int NUM_ROW_BITS = 16;
27 const int NUM_COL_BITS = 8;
28 const int NUM_FILE_BITS = (32-NUM_ROW_BITS-NUM_COL_BITS);
30 const int NUM_FILE_SHIFTS = 0;
31 const int NUM_ROW_SHIFTS = NUM_FILE_BITS;
32 const int NUM_COL_SHIFTS = NUM_ROW_BITS+NUM_FILE_BITS;
34 const int FILE_MASK = (1<<NUM_FILE_BITS)-1;
35 const int ROW_MASK = ((1<<NUM_ROW_BITS)-1)<<NUM_FILE_BITS;
36 const int COL_MASK = ((1<<NUM_COL_BITS)-1)<<(NUM_ROW_BITS+NUM_FILE_BITS);
38 public int token; // ordered triplet: (Row, Col, File Index)
40 static ArrayList source_list;
41 static Hashtable source_files;
42 static int source_count;
43 static int current_source;
44 static Hashtable sym_docs;
46 public readonly static Location Null;
48 static Location ()
50 source_files = new Hashtable ();
51 source_list = new ArrayList ();
52 current_source = 0;
53 sym_docs = new Hashtable ();
54 Null.token = 0;
57 static public void SetCurrentSource(string name)
59 int index;
61 if (!source_files.Contains (name)) {
62 index = ++source_count;
63 source_files.Add (name, index);
64 source_list.Add (name);
66 else {
67 index = (int)source_files[name];
70 current_source = index;
73 public Location (int row, int col)
75 if (row < 0 || col < 0)
76 token = 0;
77 else {
78 if (col > 255)
79 col = 255;
80 token = (current_source<<NUM_FILE_SHIFTS) + (row<<NUM_ROW_SHIFTS) + (col<<NUM_COL_SHIFTS);
84 public override string ToString ()
86 return Name + ": (" + Row + ")";
89 static public bool IsNull (Location l)
91 return l.token == 0;
94 public string Name {
95 get {
96 if(token == 0)
97 return "Internal";
99 int index = (token & FILE_MASK)>>NUM_FILE_SHIFTS;
100 string file = (string) source_list [index - 1];
101 return file;
105 public int Row {
106 get {
107 if (token == 0)
108 return 1;
110 return (token & ROW_MASK)>>NUM_ROW_SHIFTS;
114 public int Col {
115 get {
116 if (token == 0)
117 return 1;
119 return (token & COL_MASK)>>NUM_COL_SHIFTS;
123 // The ISymbolDocumentWriter interface is used by the symbol writer to
124 // describe a single source file - for each source file there's exactly
125 // one corresponding ISymbolDocumentWriter instance.
127 // This class has an internal hash table mapping source document names
128 // to such ISymbolDocumentWriter instances - so there's exactly one
129 // instance per document.
131 // This property returns the ISymbolDocumentWriter instance which belongs
132 // to the location's source file.
134 // If we don't have a symbol writer, this property is always null.
135 public ISymbolDocumentWriter SymbolDocument {
136 get {
137 ISymbolWriter sw = CodeGen.SymbolWriter;
138 ISymbolDocumentWriter doc;
140 if (token < 0)
141 return null;
143 // If we don't have a symbol writer, return null.
144 if (sw == null)
145 return null;
147 string path = Path.GetFullPath (Name);
149 if (sym_docs.Contains (path))
150 // If we already created an ISymbolDocumentWriter
151 // instance for this document, return it.
152 doc = (ISymbolDocumentWriter) sym_docs [path];
153 else {
154 // Create a new ISymbolDocumentWriter instance and
155 // store it in the hash table.
156 doc = sw.DefineDocument (path, SymLanguageType.Basic,
157 SymLanguageVendor.Microsoft,
158 SymDocumentType.Text);
160 sym_docs.Add (path, doc);
163 return doc;