2 // location.cs: Keeps track of the location of source code entity
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright 2001 Ximian, Inc.
9 // Copyright 2005 Novell, Inc.
14 using System
.Collections
.Generic
;
15 using Mono
.CompilerServices
.SymbolWriter
;
17 namespace Mono
.CSharp
{
19 /// This is one single source file.
22 /// This is intentionally a class and not a struct since we need
23 /// to pass this by reference.
25 public class SourceFile
: ISourceFile
{
26 public readonly string Name
;
27 public readonly string Path
;
28 public readonly int Index
;
29 public bool AutoGenerated
;
30 public bool IsIncludeFile
;
33 byte[] guid
, checksum
;
35 public SourceFile (string name
, string path
, int index
, bool is_include
)
40 this.IsIncludeFile
= is_include
;
43 public SourceFileEntry SourceFileEntry
{
47 SourceFileEntry ISourceFile
.Entry
{
51 public void SetChecksum (byte[] guid
, byte[] checksum
)
54 this.checksum
= checksum
;
57 public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter
)
60 file
= symwriter
.DefineDocument (Path
, guid
, checksum
);
62 file
= symwriter
.DefineDocument (Path
);
64 file
.SetAutoGenerated ();
68 public override string ToString ()
70 return String
.Format ("SourceFile ({0}:{1}:{2}:{3})",
71 Name
, Path
, Index
, SourceFileEntry
);
75 public class CompilationUnit
: SourceFile
, ICompileUnit
77 CompileUnitEntry comp_unit
;
78 Dictionary
<string, SourceFile
> include_files
;
79 Dictionary
<string, bool> conditionals
;
81 public CompilationUnit (string name
, string path
, int index
)
82 : base (name
, path
, index
, false)
85 public void AddFile (SourceFile file
)
90 if (include_files
== null)
91 include_files
= new Dictionary
<string, SourceFile
> ();
93 if (!include_files
.ContainsKey (file
.Path
))
94 include_files
.Add (file
.Path
, file
);
97 public void AddDefine (string value)
99 if (conditionals
== null)
100 conditionals
= new Dictionary
<string, bool> (2);
102 conditionals
[value] = true;
105 public void AddUndefine (string value)
107 if (conditionals
== null)
108 conditionals
= new Dictionary
<string, bool> (2);
110 conditionals
[value] = false;
113 CompileUnitEntry ICompileUnit
.Entry
{
114 get { return comp_unit; }
117 public CompileUnitEntry CompileUnitEntry
{
118 get { return comp_unit; }
121 public override void DefineSymbolInfo (MonoSymbolWriter symwriter
)
123 base.DefineSymbolInfo (symwriter
);
125 comp_unit
= symwriter
.DefineCompilationUnit (SourceFileEntry
);
127 if (include_files
!= null) {
128 foreach (SourceFile include
in include_files
.Values
) {
129 include
.DefineSymbolInfo (symwriter
);
130 comp_unit
.AddFile (include
.SourceFileEntry
);
135 public bool IsConditionalDefined (string value)
137 if (conditionals
!= null) {
139 if (conditionals
.TryGetValue (value, out res
))
142 // When conditional was undefined
143 if (conditionals
.ContainsKey (value))
147 return RootContext
.IsConditionalDefined (value);
152 /// Keeps track of the location in the program
156 /// This uses a compact representation and a couple of auxiliary
157 /// structures to keep track of tokens to (file,line and column)
158 /// mappings. The usage of the bits is:
160 /// - 16 bits for "checkpoint" which is a mixed concept of
161 /// file and "line segment"
162 /// - 8 bits for line delta (offset) from the line segment
163 /// - 8 bits for column number.
165 /// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
167 public struct Location
{
171 public readonly int LineOffset
;
172 public readonly int CompilationUnit
;
173 public readonly int File
;
175 public Checkpoint (int compile_unit
, int file
, int line
)
178 CompilationUnit
= compile_unit
;
179 LineOffset
= line
- (int) (line
% (1 << line_delta_bits
));
183 static List
<SourceFile
> source_list
;
184 static List
<CompilationUnit
> compile_units
;
185 static Dictionary
<string, int> source_files
;
186 static int checkpoint_bits
;
187 static int source_count
;
188 static int current_source
;
189 static int current_compile_unit
;
190 static int line_delta_bits
;
191 static int line_delta_mask
;
192 static int column_bits
;
193 static int column_mask
;
194 static Checkpoint
[] checkpoints
;
195 static int checkpoint_index
;
197 public readonly static Location Null
= new Location (-1);
198 public static bool InEmacs
;
203 checkpoints
= new Checkpoint
[10];
206 public static void Reset ()
208 source_files
= new Dictionary
<string, int> ();
209 source_list
= new List
<SourceFile
> ();
210 compile_units
= new List
<CompilationUnit
> ();
212 current_compile_unit
= 0;
217 // This must be called before parsing/tokenizing any files.
219 static public void AddFile (Report r
, string name
)
221 string path
= Path
.GetFullPath (name
);
223 if (source_files
.TryGetValue (path
, out id
)){
224 string other_name
= source_list
[id
- 1].Name
;
225 if (name
.Equals (other_name
))
226 r
.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name
);
228 r
.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", name
, other_name
, path
);
232 source_files
.Add (path
, ++source_count
);
233 CompilationUnit unit
= new CompilationUnit (name
, path
, source_count
);
234 source_list
.Add (unit
);
235 compile_units
.Add (unit
);
238 public static IList
<CompilationUnit
> SourceFiles
{
240 return compile_units
;
245 // After adding all source files we want to compile with AddFile(), this method
246 // must be called to `reserve' an appropriate number of bits in the token for the
247 // source file. We reserve some extra space for files we encounter via #line
248 // directives while parsing.
250 static public void Initialize ()
252 checkpoints
= new Checkpoint
[source_list
.Count
* 2];
253 if (checkpoints
.Length
> 0)
254 checkpoints
[0] = new Checkpoint (0, 0, 0);
259 line_delta_mask
= 0xFF00;
260 checkpoint_index
= 0;
261 checkpoint_bits
= 16;
265 // This is used when we encounter a #line preprocessing directive.
267 static public SourceFile
LookupFile (CompilationUnit comp_unit
, string name
)
270 if (!Path
.IsPathRooted (name
)) {
271 string root
= Path
.GetDirectoryName (comp_unit
.Path
);
272 path
= Path
.Combine (root
, name
);
276 if (!source_files
.ContainsKey (path
)) {
277 if (source_count
>= (1 << checkpoint_bits
))
278 return new SourceFile (name
, path
, 0, true);
280 source_files
.Add (path
, ++source_count
);
281 SourceFile retval
= new SourceFile (name
, path
, source_count
, true);
282 source_list
.Add (retval
);
286 int index
= (int) source_files
[path
];
287 return (SourceFile
) source_list
[index
- 1];
290 static public void Push (CompilationUnit compile_unit
, SourceFile file
)
292 current_source
= file
!= null ? file
.Index
: -1;
293 current_compile_unit
= compile_unit
!= null ? compile_unit
.Index
: -1;
294 // File is always pushed before being changed.
298 // If we're compiling with debugging support, this is called between parsing
299 // and code generation to register all the source files with the
302 static public void DefineSymbolDocuments (MonoSymbolWriter symwriter
)
304 foreach (CompilationUnit unit
in compile_units
)
305 unit
.DefineSymbolInfo (symwriter
);
308 public Location (int row
)
313 public Location (int row
, int column
)
324 int max
= checkpoint_index
< 10 ?
325 checkpoint_index
: 10;
326 for (int i
= 0; i
< max
; i
++) {
327 int offset
= checkpoints
[checkpoint_index
- i
].LineOffset
;
328 delta
= row
- offset
;
330 delta
< (1 << line_delta_bits
) &&
331 checkpoints
[checkpoint_index
- i
].File
== current_source
) {
332 target
= checkpoint_index
- i
;
337 AddCheckpoint (current_compile_unit
, current_source
, row
);
338 target
= checkpoint_index
;
339 delta
= row
% (1 << line_delta_bits
);
342 (long) (delta
<< column_bits
) +
343 (long) (target
<< (line_delta_bits
+ column_bits
));
344 token
= l
> 0xFFFFFFFF ? 0 : (int) l
;
348 static void AddCheckpoint (int compile_unit
, int file
, int row
)
350 if (checkpoints
.Length
== ++checkpoint_index
) {
351 Checkpoint
[] tmp
= new Checkpoint
[checkpoint_index
* 2];
352 Array
.Copy (checkpoints
, tmp
, checkpoints
.Length
);
355 checkpoints
[checkpoint_index
] = new Checkpoint (compile_unit
, file
, row
);
358 public override string ToString ()
360 if (column_bits
== 0 || InEmacs
)
361 return Name
+ "(" + Row
.ToString () + "):";
363 return Name
+ "(" + Row
.ToString () + "," + Column
.ToString () +
364 (Column
== column_mask
? "+):" : "):");
368 /// Whether the Location is Null
371 get { return token == 0; }
377 if (token
== 0 || index
== 0)
380 SourceFile file
= (SourceFile
) source_list
[index
- 1];
385 int CheckpointIndex
{
386 get { return (int) ((token & 0xFFFF0000) >> (line_delta_bits + column_bits)); }
393 return checkpoints
[CheckpointIndex
].LineOffset
+ ((token
& line_delta_mask
) >> column_bits
);
401 int col
= (int) (token
& column_mask
);
402 return col
== 255 ? 1 : col
;
408 return (int) (token
& column_mask
) == 255;
412 public int CompilationUnitIndex
{
416 if (checkpoints
.Length
<= CheckpointIndex
) throw new Exception (String
.Format ("Should not happen. Token is {0:X04}, checkpoints are {1}, index is {2}", token
, checkpoints
.Length
, CheckpointIndex
));
417 return checkpoints
[CheckpointIndex
].CompilationUnit
;
425 if (checkpoints
.Length
<= CheckpointIndex
) throw new Exception (String
.Format ("Should not happen. Token is {0:X04}, checkpoints are {1}, index is {2}", token
, checkpoints
.Length
, CheckpointIndex
));
426 return checkpoints
[CheckpointIndex
].File
;
430 // The ISymbolDocumentWriter interface is used by the symbol writer to
431 // describe a single source file - for each source file there's exactly
432 // one corresponding ISymbolDocumentWriter instance.
434 // This class has an internal hash table mapping source document names
435 // to such ISymbolDocumentWriter instances - so there's exactly one
436 // instance per document.
438 // This property returns the ISymbolDocumentWriter instance which belongs
439 // to the location's source file.
441 // If we don't have a symbol writer, this property is always null.
442 public SourceFile SourceFile
{
447 return (SourceFile
) source_list
[index
- 1];
451 public CompilationUnit CompilationUnit
{
453 int index
= CompilationUnitIndex
;
456 return (CompilationUnit
) source_list
[index
- 1];