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
.MonoBASIC
{
18 /// Keeps track of the location in the program
22 /// This uses a compact representation and a couple of auxiliary
23 /// structures to keep track of tokens to (line, col, file) mappings.
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
;
50 source_files
= new Hashtable ();
51 source_list
= new ArrayList ();
53 sym_docs
= new Hashtable ();
57 static public void SetCurrentSource(string name
)
61 if (!source_files
.Contains (name
)) {
62 index
= ++source_count
;
63 source_files
.Add (name
, index
);
64 source_list
.Add (name
);
67 index
= (int)source_files
[name
];
70 current_source
= index
;
73 public Location (int row
, int col
)
75 if (row
< 0 || col
< 0)
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
)
99 int index
= (token
& FILE_MASK
)>>NUM_FILE_SHIFTS
;
100 string file
= (string) source_list
[index
- 1];
110 return (token
& ROW_MASK
)>>NUM_ROW_SHIFTS
;
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
{
137 ISymbolWriter sw
= CodeGen
.SymbolWriter
;
138 ISymbolDocumentWriter doc
;
143 // If we don't have a symbol writer, 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
];
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
);