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.
15
using System.Collections.Generic;
16
using Mono.CompilerServices.SymbolWriter;
17
using System.Diagnostics;
20
namespace Mono.CSharp {
22
/// This is one single source file.
25
/// This is intentionally a class and not a struct since we need
26
/// to pass this by reference.
28
public class SourceFile : ISourceFile, IEquatable<SourceFile>
30
public readonly string Name;
31
public readonly string FullPathName;
32
public readonly int Index;
33
public bool AutoGenerated;
36
byte[] guid, checksum;
38
public SourceFile (string name, string path, int index)
42
this.FullPathName = path;
45
public SourceFileEntry SourceFileEntry {
49
SourceFileEntry ISourceFile.Entry {
53
public void SetChecksum (byte[] guid, byte[] checksum)
56
this.checksum = checksum;
59
public virtual void DefineSymbolInfo (MonoSymbolWriter symwriter)
62
file = symwriter.DefineDocument (FullPathName, guid, checksum);
64
file = symwriter.DefineDocument (FullPathName);
66
file.SetAutoGenerated ();
70
public bool Equals (SourceFile other)
72
return FullPathName == other.FullPathName;
75
public override string ToString ()
77
return String.Format ("SourceFile ({0}:{1}:{2}:{3})",
78
Name, FullPathName, Index, SourceFileEntry);
82
public class CompilationSourceFile : SourceFile, ICompileUnit
84
CompileUnitEntry comp_unit;
85
Dictionary<string, SourceFile> include_files;
86
Dictionary<string, bool> conditionals;
87
NamespaceContainer ns_container;
89
public CompilationSourceFile (string name, string fullPathName, int index)
90
: base (name, fullPathName, index)
94
CompileUnitEntry ICompileUnit.Entry {
95
get { return comp_unit; }
98
public CompileUnitEntry CompileUnitEntry {
99
get { return comp_unit; }
102
public NamespaceContainer NamespaceContainer {
107
ns_container = value;
111
public void AddIncludeFile (SourceFile file)
116
if (include_files == null)
117
include_files = new Dictionary<string, SourceFile> ();
119
if (!include_files.ContainsKey (file.FullPathName))
120
include_files.Add (file.FullPathName, file);
123
public void AddDefine (string value)
125
if (conditionals == null)
126
conditionals = new Dictionary<string, bool> (2);
128
conditionals [value] = true;
131
public void AddUndefine (string value)
133
if (conditionals == null)
134
conditionals = new Dictionary<string, bool> (2);
136
conditionals [value] = false;
139
public override void DefineSymbolInfo (MonoSymbolWriter symwriter)
141
base.DefineSymbolInfo (symwriter);
143
comp_unit = symwriter.DefineCompilationUnit (SourceFileEntry);
145
if (include_files != null) {
146
foreach (SourceFile include in include_files.Values) {
147
include.DefineSymbolInfo (symwriter);
148
comp_unit.AddFile (include.SourceFileEntry);
153
public bool IsConditionalDefined (CompilerContext ctx, string value)
155
if (conditionals != null) {
157
if (conditionals.TryGetValue (value, out res))
160
// When conditional was undefined
161
if (conditionals.ContainsKey (value))
165
return ctx.Settings.IsConditionalSymbolDefined (value);
170
/// Keeps track of the location in the program
174
/// This uses a compact representation and a couple of auxiliary
175
/// structures to keep track of tokens to (file,line and column)
176
/// mappings. The usage of the bits is:
178
/// - 16 bits for "checkpoint" which is a mixed concept of
179
/// file and "line segment"
180
/// - 8 bits for line delta (offset) from the line segment
181
/// - 8 bits for column number.
183
/// http://lists.ximian.com/pipermail/mono-devel-list/2004-December/009508.html
185
public struct Location : IEquatable<Location>
188
public readonly int LineOffset;
189
public readonly int CompilationUnit;
190
public readonly int File;
192
public Checkpoint (int compile_unit, int file, int line)
195
CompilationUnit = compile_unit;
196
LineOffset = line - (int) (line % (1 << line_delta_bits));
203
const int column_bits = 24;
204
const int line_delta_bits = 24;
208
const int column_bits = 8;
209
const int line_delta_bits = 8;
211
const int checkpoint_bits = 16;
213
// -2 because the last one is used for hidden
214
const int max_column = (1 << column_bits) - 2;
215
const int column_mask = (1 << column_bits) - 1;
217
static List<SourceFile> source_list;
218
static int current_source;
219
static int current_compile_unit;
220
static Checkpoint [] checkpoints;
221
static int checkpoint_index;
223
public readonly static Location Null = new Location (-1);
224
public static bool InEmacs;
231
public static void Reset ()
233
source_list = new List<SourceFile> ();
235
current_compile_unit = 0;
236
checkpoint_index = 0;
239
public static SourceFile AddFile (string name, string fullName)
241
var source = new SourceFile (name, fullName, source_list.Count + 1);
242
source_list.Add (source);
247
// After adding all source files we want to compile with AddFile(), this method
248
// must be called to `reserve' an appropriate number of bits in the token for the
249
// source file. We reserve some extra space for files we encounter via #line
250
// directives while parsing.
252
static public void Initialize (List<CompilationSourceFile> files)
255
source_list.AddRange (files);
257
source_list.AddRange (files.ToArray ());
260
checkpoints = new Checkpoint [source_list.Count * 2];
261
if (checkpoints.Length > 0)
262
checkpoints [0] = new Checkpoint (0, 0, 0);
265
static public void Push (CompilationSourceFile compile_unit, SourceFile file)
267
current_source = file != null ? file.Index : -1;
268
current_compile_unit = compile_unit != null ? compile_unit.Index : -1;
269
// File is always pushed before being changed.
272
public Location (int row)
277
public Location (int row, int column)
282
if (column > max_column)
285
column = max_column + 1;
290
// FIXME: This value is certainly wrong but what was the intension
291
int max = checkpoint_index < 10 ?
292
checkpoint_index : 10;
293
for (int i = 0; i < max; i++) {
294
int offset = checkpoints [checkpoint_index - i].LineOffset;
295
delta = row - offset;
297
delta < (1 << line_delta_bits) &&
298
checkpoints [checkpoint_index - i].File == current_source) {
299
target = checkpoint_index - i;
304
AddCheckpoint (current_compile_unit, current_source, row);
305
target = checkpoint_index;
306
delta = row % (1 << line_delta_bits);
310
(delta << column_bits) +
311
(target << (line_delta_bits + column_bits));
315
token = l > 0xFFFFFFFF ? 0 : (int) l;
320
public static Location operator - (Location loc, int columns)
322
return new Location (loc.Row, loc.Column - columns);
325
static void AddCheckpoint (int compile_unit, int file, int row)
327
if (checkpoints.Length == ++checkpoint_index) {
328
Array.Resize (ref checkpoints, checkpoint_index * 2);
330
checkpoints [checkpoint_index] = new Checkpoint (compile_unit, file, row);
333
string FormatLocation (string fileName)
335
if (column_bits == 0 || InEmacs)
336
return fileName + "(" + Row.ToString () + "):";
338
return fileName + "(" + Row.ToString () + "," + Column.ToString () +
339
(Column == max_column ? "+):" : "):");
342
public override string ToString ()
344
return FormatLocation (Name);
347
public string ToStringFullName ()
349
return FormatLocation (NameFullPath);
353
/// Whether the Location is Null
356
get { return token == 0; }
362
if (token == 0 || index == 0)
364
if (source_list == null || index - 1 >= source_list.Count)
365
return "unknown_file";
367
SourceFile file = source_list [index - 1];
372
public string NameFullPath {
375
if (token == 0 || index == 0)
378
return source_list[index - 1].FullPathName;
382
int CheckpointIndex {
384
const int checkpoint_mask = (1 << checkpoint_bits) - 1;
385
return ((int) (token >> (line_delta_bits + column_bits))) & checkpoint_mask;
394
int offset = checkpoints[CheckpointIndex].LineOffset;
396
const int line_delta_mask = (1 << column_bits) - 1;
397
return offset + (((int)(token >> column_bits)) & line_delta_mask);
405
int col = (int) (token & column_mask);
406
return col > max_column ? 1 : col;
412
return (int) (token & column_mask) == max_column + 1;
416
public int CompilationUnitIndex {
420
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));
421
return checkpoints [CheckpointIndex].CompilationUnit;
429
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));
430
return checkpoints [CheckpointIndex].File;
434
// The ISymbolDocumentWriter interface is used by the symbol writer to
435
// describe a single source file - for each source file there's exactly
436
// one corresponding ISymbolDocumentWriter instance.
438
// This class has an internal hash table mapping source document names
439
// to such ISymbolDocumentWriter instances - so there's exactly one
440
// instance per document.
442
// This property returns the ISymbolDocumentWriter instance which belongs
443
// to the location's source file.
445
// If we don't have a symbol writer, this property is always null.
446
public SourceFile SourceFile {
451
return (SourceFile) source_list [index - 1];
455
public CompilationSourceFile CompilationUnit {
457
int index = CompilationUnitIndex;
460
return (CompilationSourceFile) source_list [index - 1];
464
#region IEquatable<Location> Members
466
public bool Equals (Location other)
468
return this.token == other.token;
474
public class SpecialsBag
476
public enum CommentType
485
public readonly CommentType CommentType;
486
public readonly bool StartsLine;
487
public readonly int Line;
488
public readonly int Col;
489
public readonly int EndLine;
490
public readonly int EndCol;
491
public readonly string Content;
493
public Comment (CommentType commentType, bool startsLine, int line, int col, int endLine, int endCol, string content)
495
this.CommentType = commentType;
496
this.StartsLine = startsLine;
499
this.EndLine = endLine;
500
this.EndCol = endCol;
501
this.Content = content;
504
public override string ToString ()
506
return string.Format ("[Comment: CommentType={0}, Line={1}, Col={2}, EndLine={3}, EndCol={4}, Content={5}]", CommentType, Line, Col, EndLine, EndCol, Content);
510
public class PreProcessorDirective
512
public readonly int Line;
513
public readonly int Col;
514
public readonly int EndLine;
515
public readonly int EndCol;
517
public readonly Tokenizer.PreprocessorDirective Cmd;
518
public readonly string Arg;
520
public PreProcessorDirective (int line, int col, int endLine, int endCol, Tokenizer.PreprocessorDirective cmd, string arg)
524
this.EndLine = endLine;
525
this.EndCol = endCol;
530
public override string ToString ()
532
return string.Format ("[PreProcessorDirective: Line={0}, Col={1}, EndLine={2}, EndCol={3}, Cmd={4}, Arg={5}]", Line, Col, EndLine, EndCol, Cmd, Arg);
536
public readonly List<object> Specials = new List<object> ();
538
CommentType curComment;
540
int startLine, startCol;
541
System.Text.StringBuilder contentBuilder = new System.Text.StringBuilder ();
543
[Conditional ("FULL_AST")]
544
public void StartComment (CommentType type, bool startsLine, int startLine, int startCol)
547
this.startsLine = startsLine;
548
this.startLine = startLine;
549
this.startCol = startCol;
550
contentBuilder.Length = 0;
553
[Conditional ("FULL_AST")]
554
public void PushCommentChar (int ch)
558
contentBuilder.Append ((char)ch);
560
[Conditional ("FULL_AST")]
561
public void PushCommentString (string str)
563
contentBuilder.Append (str);
566
[Conditional ("FULL_AST")]
567
public void EndComment (int endLine, int endColumn)
569
Specials.Add (new Comment (curComment, startsLine, startLine, startCol, endLine, endColumn, contentBuilder.ToString ()));
572
[Conditional ("FULL_AST")]
573
public void AddPreProcessorDirective (int startLine, int startCol, int endLine, int endColumn, Tokenizer.PreprocessorDirective cmd, string arg)
575
Specials.Add (new PreProcessorDirective (startLine, startCol, endLine, endColumn, cmd, arg));
580
// A bag of additional locations to support full ast tree
582
public class LocationsBag
584
public class MemberLocations
586
public IList<Tuple<Modifiers, Location>> Modifiers { get; internal set; }
587
List<Location> locations;
589
public MemberLocations (IList<Tuple<Modifiers, Location>> mods, IEnumerable<Location> locs)
592
locations = locs != null ? new List<Location> (locs) : null;
597
public Location this [int index] {
599
return locations [index];
605
return locations != null ? locations.Count : 0;
611
public void AddLocations (params Location[] additional)
615
AddLocations ((IEnumerable<Location>)additional);
618
public void AddLocations (IEnumerable<Location> additional)
620
if (additional == null)
622
if (locations == null) {
623
locations = new List<Location>(additional);
625
locations.AddRange (additional);
630
public MemberCore LastMember {
635
Dictionary<object, List<Location>> simple_locs = new Dictionary<object, List<Location>> (ReferenceEquality<object>.Default);
636
Dictionary<MemberCore, MemberLocations> member_locs = new Dictionary<MemberCore, MemberLocations> (ReferenceEquality<MemberCore>.Default);
638
[Conditional ("FULL_AST")]
639
public void AddLocation (object element, params Location[] locations)
641
AddLocation (element, (IEnumerable<Location>)locations);
644
[Conditional ("FULL_AST")]
645
public void AddLocation (object element, IEnumerable<Location> locations)
647
if (element == null || locations == null)
649
simple_locs.Add (element, new List<Location> (locations));
652
[Conditional ("FULL_AST")]
653
public void AddStatement (object element, params Location[] locations)
657
if (locations.Length == 0)
658
throw new ArgumentException ("Statement is missing semicolon location");
659
simple_locs.Add (element, new List<Location>(locations));
662
[Conditional ("FULL_AST")]
663
public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, params Location[] locations)
669
MemberLocations existing;
670
if (member_locs.TryGetValue (member, out existing)) {
671
existing.Modifiers = modLocations;
672
existing.AddLocations (locations);
675
member_locs.Add (member, new MemberLocations (modLocations, locations));
677
[Conditional ("FULL_AST")]
678
public void AddMember (MemberCore member, IList<Tuple<Modifiers, Location>> modLocations, IEnumerable<Location> locations)
684
MemberLocations existing;
685
if (member_locs.TryGetValue (member, out existing)) {
686
existing.Modifiers = modLocations;
687
existing.AddLocations (locations);
690
member_locs.Add (member, new MemberLocations (modLocations, locations));
693
[Conditional ("FULL_AST")]
694
public void AppendTo (object existing, params Location[] locations)
696
AppendTo (existing, (IEnumerable<Location>)locations);
700
[Conditional ("FULL_AST")]
701
public void AppendTo (object existing, IEnumerable<Location> locations)
703
if (existing == null)
706
if (simple_locs.TryGetValue (existing, out locs)) {
707
simple_locs [existing].AddRange (locations);
710
AddLocation (existing, locations);
713
[Conditional ("FULL_AST")]
714
public void AppendToMember (MemberCore existing, params Location[] locations)
716
AppendToMember (existing, (IEnumerable<Location>)locations);
720
[Conditional ("FULL_AST")]
721
public void AppendToMember (MemberCore existing, IEnumerable<Location> locations)
723
if (existing == null)
725
MemberLocations member;
726
if (member_locs.TryGetValue (existing, out member)) {
727
member.AddLocations (locations);
730
member_locs.Add (existing, new MemberLocations (null, locations));
733
public List<Location> GetLocations (object element)
735
List<Location> found;
736
simple_locs.TryGetValue (element, out found);
740
public MemberLocations GetMemberLocation (MemberCore element)
742
MemberLocations found;
743
member_locs.TryGetValue (element, out found);
748
public class UsingsBag
750
public class Namespace {
751
public Location NamespaceLocation { get; set; }
752
public MemberName Name { get; set; }
754
public Location OpenBrace { get; set; }
755
public Location CloseBrace { get; set; }
756
public Location OptSemicolon { get; set; }
758
public List<object> usings = new List<object> ();
759
public List<object> members = new List<object> ();
762
public virtual void Accept (StructuralVisitor visitor)
764
visitor.Visit (this);
768
public class AliasUsing
770
public readonly Location UsingLocation;
771
public readonly Tokenizer.LocatedToken Identifier;
772
public readonly Location AssignLocation;
773
public readonly MemberName Nspace;
774
public readonly Location SemicolonLocation;
776
public AliasUsing (Location usingLocation, Tokenizer.LocatedToken identifier, Location assignLocation, MemberName nspace, Location semicolonLocation)
778
this.UsingLocation = usingLocation;
779
this.Identifier = identifier;
780
this.AssignLocation = assignLocation;
781
this.Nspace = nspace;
782
this.SemicolonLocation = semicolonLocation;
785
public virtual void Accept (StructuralVisitor visitor)
787
visitor.Visit (this);
793
public readonly Location UsingLocation;
794
public readonly MemberName NSpace;
795
public readonly Location SemicolonLocation;
797
public Using (Location usingLocation, MemberName nSpace, Location semicolonLocation)
799
this.UsingLocation = usingLocation;
800
this.NSpace = nSpace;
801
this.SemicolonLocation = semicolonLocation;
804
public virtual void Accept (StructuralVisitor visitor)
806
visitor.Visit (this);
810
public class ExternAlias
812
public readonly Location ExternLocation;
813
public readonly Location AliasLocation;
814
public readonly Tokenizer.LocatedToken Identifier;
815
public readonly Location SemicolonLocation;
817
public ExternAlias (Location externLocation, Location aliasLocation, Tokenizer.LocatedToken identifier, Location semicolonLocation)
819
this.ExternLocation = externLocation;
820
this.AliasLocation = aliasLocation;
821
this.Identifier = identifier;
822
this.SemicolonLocation = semicolonLocation;
825
public virtual void Accept (StructuralVisitor visitor)
827
visitor.Visit (this);
831
public Namespace Global {
835
Stack<Namespace> curNamespace = new Stack<Namespace> ();
839
Global = new Namespace ();
840
Global.OpenBrace = new Location (1, 1);
841
Global.CloseBrace = new Location (int.MaxValue, int.MaxValue);
842
curNamespace.Push (Global);
845
[Conditional ("FULL_AST")]
846
public void AddUsingAlias (Location usingLocation, Tokenizer.LocatedToken identifier, Location assignLocation, MemberName nspace, Location semicolonLocation)
848
curNamespace.Peek ().usings.Add (new AliasUsing (usingLocation, identifier, assignLocation, nspace, semicolonLocation));
851
[Conditional ("FULL_AST")]
852
public void AddUsing (Location usingLocation, MemberName nspace, Location semicolonLocation)
854
curNamespace.Peek ().usings.Add (new Using (usingLocation, nspace, semicolonLocation));
857
[Conditional ("FULL_AST")]
858
public void AddExternAlias (Location externLocation, Location aliasLocation, Tokenizer.LocatedToken identifier, Location semicolonLocation)
860
curNamespace.Peek ().usings.Add (new ExternAlias (externLocation, aliasLocation, identifier, semicolonLocation));
863
[Conditional ("FULL_AST")]
864
public void DeclareNamespace (Location namespaceLocation, MemberName nspace)
866
var newNamespace = new Namespace () { NamespaceLocation = namespaceLocation, Name = nspace };
867
curNamespace.Peek ().members.Add (newNamespace);
868
curNamespace.Push (newNamespace);
872
[Conditional ("FULL_AST")]
873
public void PushTypeDeclaration (object type)
876
curNamespace.Peek ().members.Add (type);
880
[Conditional ("FULL_AST")]
881
public void PopTypeDeclaration ()
886
[Conditional ("FULL_AST")]
887
public void EndNamespace (Location optSemicolon)
889
curNamespace.Peek ().OptSemicolon = optSemicolon;
893
[Conditional ("FULL_AST")]
894
public void OpenNamespace (Location bracketLocation)
896
curNamespace.Peek ().OpenBrace = bracketLocation;
899
[Conditional ("FULL_AST")]
900
public void CloseNamespace (Location bracketLocation)
902
curNamespace.Peek ().CloseBrace = bracketLocation;