2001-12-11 Ravi Pratap <ravi@ximian.com>
[mono.git] / mcs / mcs / location.cs
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 //
9
10 using System;
11 using System.Collections;
12
13 namespace CIR {
14         // <summary>
15         //   Keeps track of the location in the program
16         //
17         //   This uses a compact representation and a couple of auxiliary
18         //   structures to keep track of tokens to (file,line) mappings.
19         //
20         //   We could probably also keep track of columns by storing those
21         //   in 8 bits (and say, map anything after char 255 to be `255+').
22         // </summary>
23         public struct Location {
24                 public int token; 
25
26                 static Hashtable map;
27                 static ArrayList list;
28                 static int global_count;
29                 static int module_base;
30
31                 static void Reset ()
32                 {
33                         map = new Hashtable ();
34                         list = new ArrayList ();
35                         global_count = 0;
36                         module_base = 0;
37                 }
38                 
39                 static Location ()
40                 {
41                         Reset ();
42                 }
43         
44                 public void Push (string name)
45                 {
46                         map.Add (global_count, name);
47                         list.Add (global_count);
48                         module_base = global_count;
49                 }
50                 
51                 public Location (int row)
52                 {
53                         if (row < 0)
54                                 token = -1;
55                         else {
56                                 token = module_base + row;
57                                 global_count = token;
58                         }
59                 }
60                 
61                 //
62                 // Whether the Location is Null
63                 //
64                 static public bool IsNull (Location l)
65                 {
66                         return l.token == -1;
67                 }
68
69                 static public Location Null {
70                         get {
71                                 return new Location (-1);
72                         }
73                 }
74
75                 public string Name {
76                         get {
77                                 if (token < 0)
78                                         return "Internal";
79                                 
80                                 foreach (int b in list){
81                                         if (token < b)
82                                                 continue;
83                                         return (string) map [b];
84                                 }
85
86                                 return "Unknown";
87                         }
88                 }
89
90                 public int Row {
91                         get {
92                                 if (token < 0)
93                                         return 1;
94                                 
95                                 foreach (int b in list){
96                                         if (token < b)
97                                                 continue;
98                                         return token - b;
99                                 }
100                                 return 0;
101                         }
102                 }
103         }
104 }