2
// location.cs: Keeps track of the location of source code entity
6
// Atsushi Enomoto <atsushi@ximian.com>
7
// Marek Safar (marek.safar@gmail.com)
9
// Copyright 2001 Ximian, Inc.
10
// Copyright 2005 Novell, Inc.
14
using System.Collections.Generic;
15
using Mono.CompilerServices.SymbolWriter;
16
using System.Diagnostics;
22
// This is one single source file.
24
public class SourceFile : IEquatable<SourceFile>
27
// Used by #line directive to track hidden sequence point
30
struct LocationRegion : IComparable<LocationRegion>
32
public readonly Location Start;
33
public readonly Location End;
35
public LocationRegion (Location start, Location end)
41
public int CompareTo (LocationRegion other)
43
if (Start.Row == other.Start.Row)
44
return Start.Column.CompareTo (other.Start.Column);
46
return Start.Row.CompareTo (other.Start.Row);
49
public override string ToString ()
51
return Start.ToString () + " - " + End.ToString ();
55
public readonly string Name;
56
public readonly string FullPathName;
57
public readonly int Index;
58
public bool AutoGenerated;
61
byte[] guid, checksum;
62
List<LocationRegion> hidden_lines;
64
public SourceFile (string name, string path, int index)
68
this.FullPathName = path;
71
public SourceFileEntry SourceFileEntry {
77
public void SetChecksum (byte[] guid, byte[] checksum)
80
this.checksum = checksum;
83
public SourceFileEntry CreateSymbolInfo (MonoSymbolFile symwriter)
85
if (hidden_lines != null)
89
file = new SourceFileEntry (symwriter, FullPathName, guid, checksum);
91
file = new SourceFileEntry (symwriter, FullPathName);
93
file.SetAutoGenerated ();
99
public bool Equals (SourceFile other)
101
return FullPathName == other.FullPathName;
104
public bool IsHiddenLocation (Location loc)
106
if (hidden_lines == null)
109
int index = hidden_lines.BinarySearch (new LocationRegion (loc, loc));
112
var found = hidden_lines[index - 1];
113
if (loc.Row < found.End.Row)
120
public void RegisterHiddenScope (Location start, Location end)
122
if (hidden_lines == null)
123
hidden_lines = new List<LocationRegion> ();
125
hidden_lines.Add (new LocationRegion (start, end));
128
public override string ToString ()
130
return String.Format ("SourceFile ({0}:{1}:{2})", Name, FullPathName, Index);
135
/// Keeps track of the location in the program
139
/// This uses a compact representation and a couple of auxiliary
140
/// structures to keep track of tokens to (file,line and column)
141
/// mappings. The usage of the bits is:
143
/// - 16 bits for "checkpoint" which is a mixed concept of
144
/// file and "line segment"
145
/// - 8 bits for line delta (offset) from the line segment
146
/// - 8 bits for column number.
148
/// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
150
public struct Location : IEquatable<Location>
153
public readonly int LineOffset;
154
public readonly int File;
156
public Checkpoint (int file, int line)
159
LineOffset = line - (int) (line % (1 << line_delta_bits));
166
const int column_bits = 24;
167
const int line_delta_bits = 24;
171
const int column_bits = 8;
172
const int line_delta_bits = 8;
174
const int checkpoint_bits = 16;
176
const int column_mask = (1 << column_bits) - 1;
177
const int max_column = column_mask;
179
static List<SourceFile> source_list;
180
static int current_source;
181
static Checkpoint [] checkpoints;
182
static int checkpoint_index;
184
public readonly static Location Null = new Location ();
185
public static bool InEmacs;
192
public static void Reset ()
194
source_list = new List<SourceFile> ();
196
checkpoint_index = 0;
199
public static void AddFile (SourceFile file)
201
source_list.Add (file);
205
// After adding all source files we want to compile with AddFile(), this method
206
// must be called to `reserve' an appropriate number of bits in the token for the
207
// source file. We reserve some extra space for files we encounter via #line
208
// directives while parsing.
210
static public void Initialize (List<SourceFile> files)
212
#if NET_4_0 || MONODROID
213
source_list.AddRange (files);
215
source_list.AddRange (files.ToArray ());
218
checkpoints = new Checkpoint [System.Math.Max (1, source_list.Count * 2)];
219
if (checkpoints.Length > 0)
220
checkpoints [0] = new Checkpoint (0, 0);
223
static public void Push (SourceFile file)
225
current_source = file != null ? file.Index : -1;
226
// File is always pushed before being changed.
229
public Location (int row, int column)
234
if (column > max_column)
240
// FIXME: This value is certainly wrong but what was the intension
241
int max = checkpoint_index < 10 ?
242
checkpoint_index : 10;
243
for (int i = 0; i < max; i++) {
244
int offset = checkpoints [checkpoint_index - i].LineOffset;
245
delta = row - offset;
247
delta < (1 << line_delta_bits) &&
248
checkpoints [checkpoint_index - i].File == current_source) {
249
target = checkpoint_index - i;
254
AddCheckpoint (current_source, row);
255
target = checkpoint_index;
256
delta = row % (1 << line_delta_bits);
260
(delta << column_bits) +
261
(target << (line_delta_bits + column_bits));
265
token = l > 0xFFFFFFFF ? 0 : (int) l;
270
public static Location operator - (Location loc, int columns)
272
return new Location (loc.Row, loc.Column - columns);
275
static void AddCheckpoint (int file, int row)
277
if (checkpoints.Length == ++checkpoint_index) {
278
Array.Resize (ref checkpoints, checkpoint_index * 2);
280
checkpoints [checkpoint_index] = new Checkpoint (file, row);
283
string FormatLocation (string fileName)
285
if (column_bits == 0 || InEmacs)
286
return fileName + "(" + Row.ToString () + "):";
288
return fileName + "(" + Row.ToString () + "," + Column.ToString () +
289
(Column == max_column ? "+):" : "):");
292
public override string ToString ()
294
return FormatLocation (Name);
297
public string ToStringFullName ()
299
return FormatLocation (NameFullPath);
303
/// Whether the Location is Null
306
get { return token == 0; }
313
if (token == 0 || index <= 0)
316
SourceFile file = source_list [index - 1];
321
public string NameFullPath {
324
if (token == 0 || index <= 0)
327
return source_list[index - 1].FullPathName;
331
int CheckpointIndex {
333
const int checkpoint_mask = (1 << checkpoint_bits) - 1;
334
return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
343
int offset = checkpoints[CheckpointIndex].LineOffset;
345
const int line_delta_mask = (1 << column_bits) - 1;
346
return offset + (((int)(token >> column_bits)) & line_delta_mask);
354
return (int) (token & column_mask);
362
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));
363
return checkpoints [CheckpointIndex].File;
367
// The ISymbolDocumentWriter interface is used by the symbol writer to
368
// describe a single source file - for each source file there's exactly
369
// one corresponding ISymbolDocumentWriter instance.
371
// This class has an internal hash table mapping source document names
372
// to such ISymbolDocumentWriter instances - so there's exactly one
373
// instance per document.
375
// This property returns the ISymbolDocumentWriter instance which belongs
376
// to the location's source file.
378
// If we don't have a symbol writer, this property is always null.
379
public SourceFile SourceFile {
384
return source_list [index - 1];
388
#region IEquatable<Location> Members
390
public bool Equals (Location other)
392
return this.token == other.token;
398
public class SpecialsBag
400
public enum CommentType
410
public readonly CommentType CommentType;
411
public readonly bool StartsLine;
412
public readonly int Line;
413
public readonly int Col;
414
public readonly int EndLine;
415
public readonly int EndCol;
416
public readonly string Content;
418
public Comment (CommentType commentType, bool startsLine, int line, int col, int endLine, int endCol, string content)
420
this.CommentType = commentType;
421
this.StartsLine = startsLine;
424
this.EndLine = endLine;
425
this.EndCol = endCol;
426
this.Content = content;
429
public override string ToString ()
431
return string.Format ("[Comment: CommentType={0}, Line={1}, Col={2}, EndLine={3}, EndCol={4}, Content={5}]", CommentType, Line, Col, EndLine, EndCol, Content);
435
public class PreProcessorDirective
437
public readonly int Line;
438
public readonly int Col;
439
public readonly int EndLine;
440
public readonly int EndCol;
442
public readonly Tokenizer.PreprocessorDirective Cmd;
443
public readonly string Arg;
445
public bool Take = true;
447
public PreProcessorDirective (int line, int col, int endLine, int endCol, Tokenizer.PreprocessorDirective cmd, string arg)
451
this.EndLine = endLine;
452
this.EndCol = endCol;
457
public override string ToString ()
459
return string.Format ("[PreProcessorDirective: Line={0}, Col={1}, EndLine={2}, EndCol={3}, Cmd={4}, Arg={5}]", Line, Col, EndLine, EndCol, Cmd, Arg);
463
public readonly List<object> Specials = new List<object> ();
465
CommentType curComment;
467
int startLine, startCol;
468
System.Text.StringBuilder contentBuilder = new System.Text.StringBuilder ();
470
[Conditional ("FULL_AST")]
471
public void StartComment (CommentType type, bool startsLine, int startLine, int startCol)
475
this.startsLine = startsLine;
476
this.startLine = startLine;
477
this.startCol = startCol;
478
contentBuilder.Length = 0;
481
[Conditional ("FULL_AST")]
482
public void PushCommentChar (int ch)
486
contentBuilder.Append ((char)ch);
488
[Conditional ("FULL_AST")]
489
public void PushCommentString (string str)
491
contentBuilder.Append (str);
495
[Conditional ("FULL_AST")]
496
public void EndComment (int endLine, int endColumn)
501
Specials.Add (new Comment (curComment, startsLine, startLine, startCol, endLine, endColumn, contentBuilder.ToString ()));
504
[Conditional ("FULL_AST")]
505
public void AddPreProcessorDirective (int startLine, int startCol, int endLine, int endColumn, Tokenizer.PreprocessorDirective cmd, string arg)
508
EndComment (startLine, startCol);
509
Specials.Add (new PreProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg));
512
public void SkipIf ()
514
if (Specials.Count > 0) {
515
var directive = Specials[Specials.Count - 1] as PreProcessorDirective;
516
if (directive != null)
517
directive.Take = false;
523
// A bag of additional locations to support full ast tree
525
public class LocationsBag
527
public class MemberLocations
529
public IList<Tuple<Modifiers, Location>> Modifiers { get; internal set; }
530
List<Location> locations;
532
public MemberLocations (IList<Tuple<Modifiers, Location>> mods, IEnumerable<Location> locs)
535
locations = locs != null ? new List<Location> (locs) : null;
540
public Location this [int index] {
542
return locations [index];
548
return locations != null ? locations.Count : 0;
554
public void AddLocations (params Location[] additional)
558
AddLocations ((IEnumerable<Location>)additional);
561
public void AddLocations (IEnumerable<Location> additional)
563
if (additional == null)
565
if (locations == null) {
566
locations = new List<Location>(additional);
568
locations.AddRange (additional);
573
public MemberCore LastMember {
578
Dictionary<object, List<Location>> simple_locs = new Dictionary<object, List<Location>> (ReferenceEquality<object>.Default);
579
Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
581
[Conditional ("FULL_AST")]
582
public void AddLocation (object element, params Location[] locations)
584
AddLocation (element, (IEnumerable<Location>)locations);
587
[Conditional ("FULL_AST")]
588
public void AddLocation (object element, IEnumerable<Location> locations)
590
if (element == null || locations == null)
592
simple_locs.Add (element, new List<Location> (locations));
595
[Conditional ("FULL_AST")]
596
public void AddStatement (object element, params Location[] locations)
600
if (locations.Length == 0)
601
throw new ArgumentException ("Statement is missing semicolon location");
602
simple_locs.Add (element, new List<Location>(locations));
605
[Conditional ("FULL_AST")]
606
public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
612
MemberLocations existing;
613
if (member_locs.TryGetValue (member, out existing)) {
614
existing.Modifiers = modLocations;
615
existing.AddLocations (locations);
618
member_locs.Add (member, new MemberLocations (modLocations, locations));
620
[Conditional ("FULL_AST")]
621
public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, IEnumerable<Location> locations)
627
MemberLocations existing;
628
if (member_locs.TryGetValue (member, out existing)) {
629
existing.Modifiers = modLocations;
630
existing.AddLocations (locations);
633
member_locs.Add (member, new MemberLocations (modLocations, locations));
636
[Conditional ("FULL_AST")]
637
public void AppendTo (object existing, params Location[] locations)
639
AppendTo (existing, (IEnumerable<Location>)locations);
643
[Conditional ("FULL_AST")]
644
public void AppendTo (object existing, IEnumerable<Location> locations)
646
if (existing == null)
649
if (simple_locs.TryGetValue (existing, out locs)) {
650
simple_locs [existing].AddRange (locations);
653
AddLocation (existing, locations);
656
[Conditional ("FULL_AST")]
657
public void AppendToMember (MemberCore existing, params Location[] locations)
659
AppendToMember (existing, (IEnumerable<Location>)locations);
663
[Conditional ("FULL_AST")]
664
public void AppendToMember (MemberCore existing, IEnumerable<Location> locations)
666
if (existing == null)
668
MemberLocations member;
669
if (member_locs.TryGetValue (existing, out member)) {
670
member.AddLocations (locations);
673
member_locs.Add (existing, new MemberLocations (null, locations));
676
public List<Location> GetLocations (object element)
680
List<Location > found;
681
simple_locs.TryGetValue (element, out found);
685
public MemberLocations GetMemberLocation (MemberCore element)
687
MemberLocations found;
688
member_locs.TryGetValue (element, out found);